1. Definition
A builder is an object-creation pattern. The builder separates the construction of a complex object from its representation so that the same build process can create different representations.
2. Applicability
- When the algorithm for creating complex objects should be independent of the parts of the object and how they are assembled
- When the construction process must allow a different representation of the object being constructed
3. Structure
Builder: Specifies an abstract interface for each part of a product object that is created. (Factory method is for the entire object)
ConcreteBuilder: Implements the builder interface to construct and assemble parts of the product, defines and clarifies the representations it creates, and provides an interface to retrieve the product.
Director: Constructs an object that uses the builder interface.
Product: Represents a complex object that is constructed, ConcreteBuilder creates an internal representation of the product and defines its assembly process.
4. Illustrative examples
, the Order class is the director, which is used to produce a builder object. Mealbuilder is abstract builder, whose two subclasses are Americanmealbuilder and Chinesemealbuilder are concrete builder used to construct each part of the meal step-by-step. The meal class is product. Testmeal is a test class.
Below the source code:
Order.java:
Package Com.andy.designpattern.builder;public class Order {private Mealbuilder mealbuilder;public void Makeorder ( Mealbuilder MB) {mealbuilder = MB;} public void Createmeal () {mealbuilder.createmeal (); Mealbuilder.buildmainfood (); Mealbuilder.builddrink (); Mealbuilder.builddish (); Mealbuilder.buildsauce ();} Public Meal Getmeal () {return mealbuilder.getmeal ();}}
Mealbuilder.java:
Package Com.andy.designpattern.builder;public abstract class Mealbuilder {protected Meal Meal;/-Let child class see ITPU Blic Meal Getmeal () {return this.meal;} public void Createmeal () {meal = new meal ();} public abstract void Buildmainfood (); public abstract void Builddrink (); public abstract void Builddish (); public abstract void Buildsauce (); }
Americanmealbuilder.java:
Package Com.andy.designpattern.builder;public class Americanmealbuilder extends Mealbuilder {public void Buildmainfood () {//TODO auto-generated method Stubthis.meal.setMainFood ("fries");} public void Builddrink () {//TODO auto-generated method Stubthis.meal.setDrink ("Coke");} public void Builddish () {//TODO auto-generated method Stubthis.meal.setDish ("Hamburger");} public void Buildsauce () {//TODO auto-generated method Stubthis.meal.setSause ("Ketchup");}}
Chinesemealbuilder.java:
Package Com.andy.designpattern.builder;public class Chinesemealbuilder extends Mealbuilder {public void Buildmainfood ( {//TODO auto-generated method Stubthis.meal.setMainFood ("Rice,noodles");} public void Builddrink () {//TODO auto-generated method Stubthis.meal.setDrink ("Soup");} public void Builddish () {//TODO auto-generated method Stubthis.meal.setDish ("vegetables and Meat");} public void Buildsauce () {//TODO auto-generated method Stubthis.meal.setSause ("Peanut Sause");}}
Meal
Package Com.andy.designpattern.builder;public class Meal {private string mainfood;private string Drink;private string Dish;private string Sause;public string Getmainfood () {return mainfood;} public void Setmainfood (String mainfood) {this.mainfood = Mainfood;} Public String Getdrink () {return drink;} public void Setdrink (String drink) {this.drink = drink;} Public String Getdish () {return dish;} public void Setdish (String dish) {this.dish = dish;} Public String Getsause () {return sause;} public void Setsause (String sause) {this.sause = Sause;} Public String toString () {return ' Mainfood: ' +mainfood+ ' \ndrink: ' +drink+ ' \ndish: ' +dish+ ' \nsause: ' +sause+ ' \ n ';}}
Testmeal.java:
Package Com.andy.designpattern.builder;public class Testmeal {public static void main (string[] args) {Mealbuilder Chinesebuilder = new Chinesemealbuilder (); Mealbuilder Americanbuilder = new Americanmealbuilder (); Order order = New Order (); Order.makeorder (chinesebuilder); o Rder.createmeal (); Meal Meal1 = Order.getmeal (); Order.makeorder (Americanbuilder); Order.createmeal (); Meal Meal2 = Order.getmeal (); System.out.println ("Chinese: \ n" +meal1.tostring ()); System.out.println ("American: \ n" +meal2.tostring ());}}
The console output is:
Chinese:mainfood:rice,noodlesdrink:soupdish:vegetables and Meatsause:peanut SauseAmerican:MainFood:friesDrink: CokeDish:hamburgerSause:ketchup
As we can see from the example, we've just passed in a different type of Mealbuilder reference to the Director (Order Class), and we've got a different product from the generator. The generator pattern hides the details of the product implementation well, separating the construction and presentation. The generator pattern often divides the product into many parts in the director, then builds the product in part of the builder, and then pulls the product out of the builder.
5. Related modes
The abstract factory pattern is similar to the generator pattern, and can be used to create complex objects. The main difference is that the generator pattern constructs the object step at a stage, and the product returns at the last step, while the abstract factory is producing a series of (often multiple) products and returning the product immediately.
Resources:
1. Wikipedia: Generator mode
2.tutorialspoint:builder Pattern
3.Build Design Pattern
Design mode (iii) generator (builder)