Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Tutorials
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments


 
 

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.

Lets consider this class as Pizza.java

package builder;

public abstract class Pizza implements Item {

public Packing pack() {
return new Wrapper();
}
public abstract int price();
}


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.

MealBuilder.java

package builder;

public class MealBuilder {
public Packing additems() {
Item[] items = {new Italianpizza(), new IceCream(), new Coke()}

return new MealBox().addItems(items);
}

public int CalculatePrice() {
int totalPrice = new Italianpizza().price() + new Coke().price() + new IceCream().price();

return totalPrice;
}
}


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.

 

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Tutorials:
J2ME Event Handling Ex
J2ME HashTable Example
J2ME Icon MIDlet Examp
J2ME Image Item Exampl
J2ME Image Example
J2ME Item State Listen
J2ME Key Codes Example
J2ME KeyEvent Example
J2ME Label Example
J2ME Random Number
J2ME Read File
J2ME RMS Sorting Examp
J2ME Timer MIDlet Exam
Custom Item in J2ME
Creational Design Patt
Design Patterns
Throwing Run time exce
Grid in Echo3
Creating Table in Echo
JPA Introduction
Java bigdecimal toBigI
Java bigdecimal shortV
Java bigdecimal shortV
Java bigdecimal signum
Java bigdecimal stripT
Java bigdecimal subtra
Java bigdecimal subtra
Java bigdecimal toBigI
Java bigdecimal toEngi
Java bigdecimal toPlai
Java bigdecimal toStri
Java bigdecimal ulp ex
Java bigdecimal unscal
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal setSca
Java bigdecimal setSca
Java bigdecimal scaleB
Java bigdecimal scale
Java bigdecimal round
Java bigdecimal remain
Java bigdecimal remain
Java bigdecimal precis
Java bigdecimal pow me
Java bigdecimal pow ex
Java bigdecimal plus m
Java bigdecimal plus e
Java bigdecimal negate
Java bigdecimal negate
Java bigdecimal multip
Java bigdecimal multip
Java BigDecimal movePo
Java bigdecimal movePo
Java bigdecimal min ex
Java bigdecimal max ex
CheckBox component in
Visibility of Componen
Loading delay componen
Simple input applicati
Opening a new window i
Hello World in Echo3 f
Use of Local Inner cla
JSP bean set property
Java Method Synchroniz
Java Method Return Val
JAVA Method Wait
JDBC vs ORM
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal double
Java BigDecimal equals
Java BigDecimal hashCo
Java BigDecimal intVal
Java BigDecimal intVal
Java BigDecimal longVa
Java BigDecimal longVa
Java BigDecimal abs ex
Java BigDecimal add ex
Java BigDecimal byteVa
Java Bigdecimal class
Java BigInteger long
Java BigDecimal compar
Java divide method exa
Java BigDecimal floatV
Appending Image into t
J2ME Convert Date To S
Appending string in J2
J2ME Enumeration Examp
J2ME Display Size Exam
J2ME Current Date And
J2ME Form Class
Creating Dynamic Tree
Introduction to RCFace
JSP bean get property
Java bean example in J
J2ME Record Store Exam
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.