Builder Pattern

Ads

This design pattern allows the client to construct a complex object based on its type and content.

Builder Pattern

Builder Pattern:

This design pattern allows
the client to construct a complex object based on its type and content. One can
achieve the way of constructing the objects by using the factory pattern. This
is similar to the abstract factory pattern as both returns a group of related
objects. The only difference between these two patterns is that Builder pattern
makes a complex object step by step on the basis of data passed to it.

Benefits: These types of patterns provide greater control
over construction process, separation between the construction and
representation, and support to change the internal representation of
objects.

Usage: Builder is useful in those conditions
where you need to assemble several different kinds of complex objects at
run-time. Once it is created it isolates the building process of object, from
the object itself. Different objects may be constructed by using similar methods
but once the object is constructed it may exhibit different behavior.

First lets create an interface named Item which
contains the two public methods one for packaging the item and other for
defining the price of each item.

We are putting all the classes in the package builder.

package builder;

public interface Item {

public Packing pack();

public int price();

}

Now, we define the classes for each of the item, as pizza, ice cream and cold drink. All these classes will implement the Item interface.

Lets start with Pizza. Here we are making the pizza.java class as abstract
because we will implement price method according the type of the
pizza. A pizza is wrapped in the paper and is served. The class Wrapper is sub-class of Packing interface.

Now the class pizza further extended to Italianpizza, Cheesepizza etc. All these
classes will implement the price() method and return a price for each type of
pizza. In this example we are implementing the Italianpizza class of the Pizza
class.

Italianpizza.java

package builder;

public class Italianpizza extends Pizza {

public int price() {
return 200;
}
}

Now lets consider the item Ice Cream

IceCream.java

package builder;

public class IceCream implements Item {

public Packing pack() {
return new Envelop();
}

public int price() {
return 20;
}
}

Now, let?s see the Builder class, MealBuilder. This is the class which serves the meal.

This class calculates the total meal and its total price. Here, we have
extracted the price calculation and meal package building activity, that is a meal box. The Builder pattern hides the internal details of how the product is built.
Since each builder is independent of others therefore it improves modularity and makes the building of other builders easy.
Because, each builder builds the final product step by step, we have more control on the final product.

The conclusion is that in Builder Pattern, the client
instructs the builder class what it needed and asks for the result, the client
is not interested how the builder class will create the objects.