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 complex construction is when the object to be constructed is composed of different other objects which are represented by abstractions.
Consider a menu in McDonald's. A menu contains a drink, a main and a side. Depending on which descendants of the individual abstractions are composed together, the created menu has another representation.
There, we got two instances of the menu with different representations. The process of construction in turn remains the same. You create a menu with a drink, a main and a side.
By using the builder pattern, you separate the algorithm of creating a complex object from the different components used to create it.
In terms of the builder pattern, the algorithm is encapsulated in the director whereas the builders are used to create the integral parts. Varying the used builder in the algorithm of the director results in a different representation because other parts are composed to a menu. The way a menu is created remains the same.
IMHO
Builder is some kind of more complex Factory.
But in Builder you can instantiate objects with using another factories, that are required to build final and valid object.
So, talking about "Creational Patterns" evolution by complexity you can think about it in this way:
Both patterns come for the same necessity: Hide from some client code the construction logic of a complex object. But what makes "complex" (or, sometimes, complicate) an object? Mainly, it's due to dependencies, or rather the state of an object composed by more partial states. You can inject dependencies by constructor to set the initial object state, but an object may require a lot of them, some will be in a default initial state (just because we should have learned that set a default dependency to null is not the cleanest way) and some other set to a state driven by some condition. Moreover, there are object properties that are some kind of "oblivious dependencies" but also they can assume optional states.
there are two well known ways to dominate that complexity:
Composition/aggregation: Construct an object, construct its dependent objects, then wire together. Here, a builder can make transparent and flexible the process that determines the rules that lead the construction of component.
Polymorphism: Construction rules are declared directly into subtype definition, so you have a set of rules for each subtype and some condition decides which one among these set of rules apply to construct the object. A factory fits perfectly in this scenario.
Nothing prevents to mix these two approaches. A family of product could abstract object creation done with a builder, a builder could use factories to determine which component object instantiate.
Factory: Used for creating an instance of an object where the dependencies of the object are entirely held by the factory. For the abstract factory pattern, there are often many concrete implementations of the same abstract factory. The right implementation of the factory is injected via dependency injection.
Builder: Used to build immutable objects, when the dependencies of the object to be instantiated are partly known in advance and partly provided by the client of the builder.
An example
1) using abstract factory:
2) using builder:
As there is no WindowsButton class, he (the builder) must be in charge of correctly building the button, ie:
button.setOS = windows
.It is similar to comparing TPH vs TPT in db design.
Factory pattern creates a concrete implementation of a class at runtime, i.e its main intention is to use polymorphism to allow subclasses decide which class to instantiate. This means at compile time we dont know the exact class that will be created, while Builder pattern is mainly concerned with solving the problem of telescoping constructors antipattern, which arises due to a large number of optional fields of a class. In builder pattern there is no notion of polymorphism, as we know what object we are trying to construct at compile time.
The only common theme of these two patterns is the hiding of constructors and object creation behind factory methods, and the build method, for improved object construction.