Design Pattern Part – 5.

Builder Design Pattern falls under the type of creational pattern category.

Just like a house construction follows an ordered process of construction namely

1. Foundation

2. Wall Door & window construction.

3. Roof construction.

Similarly a build pattern follows a ordered process to construct objects. E.g. DB normalization before domain objects and business layer is constructed.

 Figure: – Builder concept

The Builder Design Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.

The Builder pattern requires that you define an interface, which will be used by clients to build complex objects in bits and pieces. Builder pattern is useful when the construction of the object is very complex.

The main objective is to separate the construction of objects and their representations. If we are able to separate the construction and representation, we can then get many representations from the same construction.

Consider we have application where we need the same report to be displayed in either ‘PDF’ or ‘WORD’ format. Figure ‘Request a report’ shows the series of steps to achieve the same. Depending on report type a new report is created, report type is set, headers and footers of the report are set and finally we get the report for display. The construction process is same for both the types of reports but they result in different representations.

We will take the same report problem and try to solve the same using builder patterns. There are three main parts when you want to implement builder patterns.

• Builder: – Builder is responsible for defining the construction process for individual parts. Builder has those individual processes to initialize and configure the product.
• Director: – Director takes those individual processes from the builder and defines the sequence to build the product.
• Product: – Product is the final object which is produced from the builder and director coordination.

First let’s have a look at the builder class hierarchy. We have a abstract class called as ‘ReportBuilder’ from which custom builders like ‘ReportPDF’ builder and ‘ReportWORD’ builder will be built.

Figure ‘Builder classes in actual code’ shows the methods of the classes. To generate report we need to first Create a new report, set the report type (to WORD or PDF), set report headers, set the report footers and finally get the report. We have defined two custom builders one for ‘PDF’ (ReportPDF) and other for ‘WORD’ (ReportWord). These two custom builders define their own process according to the report type.

Figure: – Builder classes in actual code

Now let’s understand how director will work. Class ‘jpDirector’ takes the builder and calls the individual method process in a sequential manner. So director is like a driver who takes all the individual processes and calls them in sequential manner to generate the final product, which is the report in this case. Figure ‘Director in action’ shows how the method ‘MakeReport’ calls the individual process to generate the report product by PDF or WORD.

Figure: – Director in action

The third component in the builder is the product which is nothing but the report class in this case.

Figure: – The report class

Now let’s take a top view of the builder project. Figure ‘Client,builder,director and product’ shows how they work to achieve the builder pattern. Client creates the object of the director class and passes the appropriate builder to initialize the product. Depending on the builder the product is initialized/created and finally sent to the client.

Figure: – Client, builder, director and product 

I hope you enjoyed reading this blog from Design Pattern Series, in my next blog I will be discussing about “Chain of Responsibility” Design Pattern till then happy reading.


2 thoughts on “Design Pattern Part – 5.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s