Factory

Definition: A Factory is a mechanism for encapsulating complex creation logic and abstracting the type of a created object for the sake of a client.

Problem: Creation of an object can be a major operation in itself, but complex assembly operations do not fit the responsibility of the created objects. Combining such responsibilities can produce ungainly designs that are hard to understand. Making the client direct construction muddies the design of the client, breaches encapsulation of the assembled object or Aggregate, and overly couples the client to the implementation of the created object.

Solution: Shift the responsibility for creating instances of complex objects and Aggregates to a separate object, which may itself have no responsibility in the domain model but is still part of the domain design. Provide an interface that encapsulates all complex assembly and that does not require the client to reference the concrete classes of the objects being instantiated. Create entire Aggregates as a piece, enforcing their invariants.

See also: Factory, Entity, Value Object