What is the difference between the Builder design pattern and the Factory design pattern?
Which one is more advantageous and why ?
How do I represent my findings as a graph if I want to test and compare/contrast these patterns ?
What is the difference between the Builder design pattern and the Factory design pattern?
Which one is more advantageous and why ?
How do I represent my findings as a graph if I want to test and compare/contrast these patterns ?
Telescoping Constructor Pattern
Analogy:
Courtesy
Both are very much similar , but if you have a large number of parameters for object creation with some of them optional with some default values , go for Builder pattern.
The Factory pattern can almost be seen as a simplified version of the Builder pattern.
In the Factory pattern, the factory is in charge of 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. An example of a factory method
createCar
might return aFord
or aHonda
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.
To continue the car example you might have a
createCar
builder method which creates aHonda
-typed object with a 4 cylinder engine, or aHonda
-typed object with 6 cylinders. The builder pattern allows for this finer granularity.Diagrams of both the Builder pattern and the Factory method pattern are available on Wikipedia.
Both are Creational patterns, to create Object.
1) Factory Pattern - Assume, you have one super class and N number of sub classes. The object is created depends on which parameter/value is passed.
2) Builder pattern - to create complex object.
Build pattern emphasizes on complexity of creating object (solved by "steps")
Abstract pattern emphasizes "just" on "abstraction" of (multiple but related) objects.
Difference is clear In builder pattern, builder will create specific type of object for you. You have to tell what builder has to build. In factory pattern , using abstract class you are directly building the specific object.
Here builder class acts as mediator between main class and specific type classes. More abstraction.