Author: Aiesha Adnan
Motivation for Learning(Problem)
In the class I learned about the Factory pattern.This pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. However it doesnot have a mechanism to separate the construction of complex object from its representations and provide more complex objects.The Factory patterns idea is about what is created.Not how it is created. Therefore I had an idea to learn about a design pattern which provides the step by step procedure in creating the final product,depending on data,where I will have more control over a product that is build. Then I came across the Builder Pattern which is coming under creational pattern, which can show me the details of the objects that I create ,which is absent in factory pattern.
What is Builder Pattern(insight Solution)
The Builder Pattern assembles a number of objects, such as display widgets, in various ways depending on the data. Furthermore, since Java is one of the few languages where you can cleanly separate the data from the display methods into simple objects, Java is the ideal language to implement Builder patterns.
The builder pattern can be used when
• You need to keep the algorithm of creating a complex object separate from the parts that make it up.
• The construction process must allow different representations for the object that’s constructed
This is what I am exactly looking for.
Implementing Builder pattern(plan of achieving the solutions encountered)
Decide if a common input and many possible representations (or outputs) is the problem at hand.
Encapsulate the parsing of the common input in a Director class.
- Design a standard protocol for creating all possible output representations.
- Capture the steps of this protocol in a Builder interface.
- Define a Builder derived class for each target representation.
- The client creates a Director object and a Builder object, and registers the latter with the former.
- The client asks the Director to "construct".
- The client asks the Builder to return the result.
Creating a director
The Director in the pattern is represented by Waiter . The director ensures that the steps are executed in the right order. In our case the director, an experienced Waiter , knows the order in which the steps must be executed in order to ensure that the pizza is made properly and orders the pizza builder to carry out each step in turn. So he may call the pizza builder and tell him to get going, then call out the following instructions in the constructor of this class which the pizza builder must execute in the following order.
/** "Director" */
class Waiter
{
private PizzaBuilder pizzaBuilder;
public void setPizzaBuilder(PizzaBuilder pb)
{
pizzaBuilder = pb;
}
public Pizza getPizza()
{
return pizzaBuilder.getPizza();
}
public void constructPizza()
{
pizzaBuilder.createNewPizzaProduct();
pizzaBuilder.buildDough();
pizzaBuilder.buildSauce();
pizzaBuilder.buildTopping();
}
}
Note something very important here though. The PizzaBuilder has a communication problem and only gets along with people he has known for a long time. So we shield the PizzaBuilder from the flightiness of the Specialist PizzaBuilder and he only deals with the PizzaBuilder who has been around since the restaurant started. So in reality, he speaks to the Specialist PizzaBuilder through the PizzaBuilder . He never knows the specialist PizzaBuilder on duty and doesn't care. Now the PizzaBuilder implements the step that she can, but passes on the specialized steps to the Specialist. In so doing, we get our pizza made, but without any direct contact between the PizzaBuilder (Director) and Specialist PizzaBuilder (Abstract Builder). This is just as well because they are easily offended and the fact that the PizzaBuilder cannot remember their names is very offensive to each of them.
Creating a Builder
The Abstract-Builder in the pattern is represented by a PizzaBuilder,who knows ALL the steps which are needed for composing the complex object. Because all our specialist PizzaBuildersare familiar with the steps needed for making the pizza, it ensures that there is expertise in all the steps to be executed. The PizzaBuilder interviews all specialist Pizza Builders before they can be employed in the restaurant. She ensures that the Specialist PizzaBuilder is expert in all the steps which are specific to the kind of pizza they are supposed to be making. The Abstract Builder source code shows the steps.It doesn’t need to be in order.Note also that some of the steps are already filled in.
/** "Abstract Builder" */
abstract class PizzaBuilder
{
protected Pizza pizza;
public Pizza getPizza()
{
return pizza;
}
public void createNewPizzaProduct()
{
pizza = new Pizza();
}
public abstract void buildDough();
public abstract void buildSauce();
public abstract void buildTopping();
}
Creating the concrete builder
Each type of product (pizza) which can be created is tied to one specialist Pizza Builder. He knows how to carry out each of the steps in the general recipe for the particular type of pizza he makes and so we can consider him to be an extension of the Pizza Builder. However each pizza Builder can make only the type of pizza which he specializes in. So here I have have the HawaiianPizzaBuilder and SpicyPizzaBuilder.Here HawaiianPizzaBuilder can make Hawaiian Pizza and the SpicyPizzaBuilder must know how to carry out all the steps which apply to the making of a Spicy Pizza . In reality he only needs to know the steps that the SpicyPizzaBuilder and the HawaiianPizzaBuilder needs to know are represented in the source code of each class. The specialist PizzaBuilder can override the PizzaBuilder . Since knowledge of the steps must reflect all the steps as outlined by the PizzaBuilder, it ensures that the Specialist PizzaBuilder is in a position to do all the steps. The great thing though is that we can customize the pizza we make by changing the Specialist PizzaBuilder we use to make it. Because all specialists know the steps relevant to their pizza, it ensures our pizza is made completely. Each specific PizzaBuilder in this example reflects the role of the concrete-builder in the Builder pattern. Note very importantly, that in this example, the specialist PizzaBuilder do not know the order in which the steps are carried out. As we will see, the PizzaBuilder knows the order of the steps and will take care of the sequencing requirement.
/** "ConcreteBuilder" */
class HawaiianPizzaBuilder extends PizzaBuilder
{
public void buildDough()
{
pizza.setDough("cross");
}
public void buildSauce()
{
pizza.setSauce("mild");
}
public void buildTopping()
{
pizza.setTopping("ham+pineapple");
}
}
/** "ConcreteBuilder" */
class SpicyPizzaBuilder extends PizzaBuilder
{
public void buildDough()
{
pizza.setDough("pan baked");
}
public void buildSauce()
{
pizza.setSauce("hot");
}
public void buildTopping()
{
pizza.setTopping("pepperoni+salami");
}
}
Creating the Product
The complex object in our example is the pizza and it reflects the role of the concrete product in the builder pattern. Remember each concrete product is built by only one builder. In fact selecting the builder is in effect, selecting the product. So when we select the SpicyPizzaBuilder , we are guaranteed to get back a Spicy pizza .
/** "Product" */
class Pizza
{
private String dough = "";
private String sauce = "";
private String topping = "";
public void setDough(String dough)
{ this.dough = dough; }
public void setSauce(String sauce)
{ this.sauce = sauce; }
public void setTopping(String topping)
{ this.topping = topping; }
}
Creating the Client
The client in the pattern is similar to the customer of the restaurant. In the case of the customer, an order for a particular type of pizza (or concrete product) is made. The client in the pattern knows all about the available products and knows which concrete-builder is responsible for each product. In fact the customers customers even know the names of the specialist pizzaBuilders who work for the restaurant and the type of specialty pizza that they make.In the class below the customer orders for a “hawaiianPizza”,look at the codes.
/** A customer ordering a pizza. */
class BuilderExample
{
public static void main(String[] args)
{
String top = "pepperoni+salami";
Waiter waiter = new Waiter();
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
waiter.setPizzaBuilder( hawaiianPizzaBuilder );
waiter.constructPizza();
Pizza pizza = waiter.getPizza();
}
}
Implementation issues to consider ( their solutions too)
- Assembly and construction interface (interface must be general enough, intermediate access may be needed).
- Why no abstract class for products? (usually, the products are too Different)
- Empty methods as default in Builder (clients only override operations of interest.

Conclusion
Through this learning journal I have gained the knowledge of the the builder pattern and the differences between builder pattern and abstract factory pattern.
The intention of the Builder Pattern is to separate the construction of a complex object from its representation so that the same construction process can create different representations.
The builder pattern process
The "director" invokes "builder" services as it interprets the external format. The "builder" creates part of the complex object each time it is called and maintains all intermediate state. When the product is finished, the client retrieves the result from the "builder".
Since Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
Significant differences between builder and Factory and Abstract Factory Pattern.
• The factory pattern defers the choice of what concrete type of object to make until run time.
The builder pattern encapsulates the logic of how to puttogether a complex object so that the client just requests a configuration and the builder directs the logic of building it.
• The factory is concerned with what is made, the builder with how it is
made.
Abstract factory is similar to builder in that it too may construct complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract factor's emphasis is on families of product objects(either simple or complex).
• Builder returns the product as the final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
Total Hours Spent in learning: 13 Hours 10 minutes
No comments:
Post a Comment