Builder
This is used to create objects and is really useful in the situation when the object constructor parameters combination leads to numerous constructors. The builder pattern receives the attributes or initialization parameters step by step and ultimately constructs the object using those parameters and returns the desired object.
Avoiding the situation of having numerous constructors is often described as the solution to the telescoping constructor anti-pattern.
For better illustration, I am using the image I borrowed from references:
To explain the diagram, the Director module is like the client module which asks the Builder to build the Product. The Builder uses the buildPart() (can be many buildPart() methods for many different attributes) to build the Product by accumulating the attributes. Finally the getResult() method is responsible to return the Product which comprises of all the attributes accumulated by the Builder so far.
The source code can be found in GitHub.
PS: Another interesting example can be found here.
References:
This is used to create objects and is really useful in the situation when the object constructor parameters combination leads to numerous constructors. The builder pattern receives the attributes or initialization parameters step by step and ultimately constructs the object using those parameters and returns the desired object.
Avoiding the situation of having numerous constructors is often described as the solution to the telescoping constructor anti-pattern.
For better illustration, I am using the image I borrowed from references:
To explain the diagram, the Director module is like the client module which asks the Builder to build the Product. The Builder uses the buildPart() (can be many buildPart() methods for many different attributes) to build the Product by accumulating the attributes. Finally the getResult() method is responsible to return the Product which comprises of all the attributes accumulated by the Builder so far.
The source code can be found in GitHub.
PS: Another interesting example can be found here.
References:

No comments:
Post a Comment