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 ?
A factory is simply a wrapper function around a constructor (possibly one in a different class). The key difference is that a factory method pattern requires the entire object to be built in a single method call, with all the parameters pass in on a single line. The final object will be returned.
A builder pattern, on the other hand, is in essence a wrapper object around all the possible parameters you might want to pass into a constructor invocation. This allows you to use setter methods to slowly build up your parameter list. One additional method on a builder class is a build() method, which simply passes the builder object into the desired constructor, and returns the result.
In static languages like Java, this becomes more important when you have more than a handful of (potentially optional) parameters, as it avoids the requirement to have telescopic constructors for all the possible combinations of parameters. Also a builder allows you to use setter methods to define read-only or private fields that cannot be directly modified after the constructor has been called.
Basic Factory Example
Basic Builder Example
It may be worth comparing the code samples from these two wikipedia pages:
http://en.wikipedia.org/wiki/Factory_method_pattern
http://en.wikipedia.org/wiki/Builder_pattern
The builder design pattern describes an object that knows how to craft another object of a specific type over several steps. It holds the needed state for the target item at each intermediate step. Think what StringBuilder goes through to produce a final string.
The factory design pattern describes an object that knows how to create several different but related kinds of object in one step, where the specific type is chosen based on given parameters. Think of the serialization system, where you create your serializer and it constructs the desired in object all in one load call.
Constructing a complex object step by step : builder pattern
A simple object is created by using a single method : factory method pattern
Creating Object by using multiple factory method : Abstract factory pattern
Builder and Abstract Factory have meant for different purposes. Depending on right use case, you have to select suitable design pattern.
Builder salient features:
Factory (simple Factory) salient features:
Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex)
Have a look at related posts:
Keeping builder in separate class (fluent interface)
Design Patterns: Factory vs Factory method vs Abstract Factory
You can refer to below articles for more details:
sourcemaking
journaldev
Abstract Factory & Builder pattern are both Creational patterns but with different intent.
Abstract Factory Pattern emphasizes object creation for families of related objects where:
Builder pattern focuses on constructing a complex object step by step. It decouples the representation from the process of constructing the complex object, so that the same construction process can be used for different representations.
One striking difference between Builder & factory which I could make out was the following
suppose we have a car
In the above interface we can get car by the following way :
but what if, some exception happens while creating the Seats ??? YOU WILL NOT GET THE OBJECT AT ALL // BUT
suppose you have implementation like the following
Now you can create like this
Here in the second case , even if one operation fails you would still get the Car.
May be that car does not works perfectly later but , you would have the object.
Because Factory Method gives you the Car in single call , whereas the Builder builds one by one.
Although, It depends on the needs of the deign which one to go for.