The Factory pattern can almost be seen as a simplified version of the Builder pattern.
In the Factory pattern, the factory is responsible for creating various subtypes of an object depending on the needs.
The user of a factory method doesn't need to know the exact subtype of that object.
For example, a factory method createCar()
might return a Ford or a Honda typed object.
In the Builder pattern, different subtypes are also created by a builder method,
but the composition of the objects might differ within the same subclass.
Continuing the car example:
You might have a createCar()
builder method which creates:
- A Honda-typed object with a 4-cylinder engine, or
- A Honda-typed object with 6 cylinders.
The builder pattern allows for this finer granularity.