Tuesday, October 30, 2012

Builder Pattern

Builder Pattern
If I say that lets not go through this pattern because you already know it, you might not believe me. Ok so now let me tell you a little secret, if you know factory method aka virtual construction mechanism you know all creational pattern. Well all the creational patterns actually create a factory object to create the product object. Abstract factory creates a factory object to create a family of related or dependent product. Similarly builder and prototype also create a factory object and then create the product using factory method. Its just that the these patterns can be used in special situations like abstract factory can be used to create family of related products.


Ok so let us jot down the builder pattern and when it should be used. First see the bookish definition.



Builder pattern: Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Well this definition, I feel, can be applied to all the creational patterns, but what is so special about builder pattern.

Well what factory pattern does, it creates a factory and that factory creates the product but if the creation of the product itself is a very complex process then what. Well builder pattern suits for those situations only.

If the creation of a product is very complex process then just separate out the creation of parts and assembling of part.

Ok so lets just go step by step.

1) The first thing that we do is to create in interface to create the parts of the object lets call it IBuilder.
2) Create a concrete builder which will do three major things first, create the product parts by implementing the IBuilder and secondly,it assembles all these parts and keep a track of the representation of the product it created and finally, it provides an interface to retrieve the product.
3) Create an object which will use the builder to create the product. The application will use this object to use the builder and create products. This object is called as director.

Well that’s all we need, let me just put a picture which will show what I have written



Now let us see how to use this pattern, step by step
1) The client creates the Director object and configures it with the desired Builder object.
2) Director notifies the builder whenever a part of the product should be built.
3) Builder handles requests from the director and adds parts to the product.
4) The client retrieves the product from the builder.
Well I am not putting any code here because I myself have not implemented it as the above diagram makes it pretty clear what code should be written and we already know the main part i.e. factory method.

Ok now, when should this pattern be used.

• The algorithm for creating a complex subsystem shall be independent of the parts that make up the subsystem and how the parts are assembled.
• The creation process for a complex subsystem shall support multiple representations of that subsystem.
Looks fairly simple, if you know the factory method if not then may be lets just go through the factory pattern again and then revisit this fellow.

No comments:

Post a Comment