Search: in
Builder pattern
Builder pattern in Encyclopedia Encyclopedia
  Tutorials     Encyclopedia     Videos     Books     Software     DVDs  
       





Builder pattern

The builder pattern is an object creation software design pattern. The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects. Often, the builder pattern is used to build products in accordance with the composite pattern.

Contents


Definition

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations. [1]

Structure

Builder Structure
Builder Structure

Builder
Abstract interface for creating objects (product).
Concrete Builder
Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles parts to build the objects.
Director
The Director class is responsible for managing the-correct sequence of object creation. It receives a Concrete Builder as a parameter and executes the necessary operations on it.
Product
The final object that will be created by the Director using Builder.

Useful tips

  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Builder often builds a Composite.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
  • Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components are built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
  • Builders are good candidates for a fluent interface.

Examples

Java

  /** "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; }  }      /** "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();  }    /** "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"); }  }      /** "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();     }  }      /** A customer ordering a pizza. */  class BuilderExample {     public static void main(String[] args) {         Waiter waiter = new Waiter();         PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();         PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();           waiter.setPizzaBuilder( hawaiianPizzaBuilder );         waiter.constructPizza();           Pizza pizza = waiter.getPizza();     }  } 

C#

 using System;  namespace BuilderPattern {   // Builder - abstract interface for creating objects (the product, in this case)   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();   }   // Concrete Builder - provides implementation for Builder; an object able to construct other objects.   // Constructs and assembles parts to build the objects   class HawaiianPizzaBuilder : PizzaBuilder   {     public override void BuildDough()     {       pizzaDough = "cross";     }      public override void BuildSauce()     {       pizzaSauce = "mild";     }      public override void BuildTopping()     {       pizzaTopping = "ham+pineapple";     }   }   // Concrete Builder - provides implementation for Builder; an object able to construct other objects.   // Constructs and assembles parts to build the objects   class SpicyPizzaBuilder : PizzaBuilder   {     public override void BuildDough()     {       pizzaDough = "pan baked";     }      public override void BuildSauce()     {       pizzaSauce = "hot";     }      public override void BuildTopping()     {       pizzaTopping = "pepperoni + salami";     }   }     // Director - responsible for managing the correct sequence of object creation.   // Receives a Concrete Builder as a parameter and executes the necessary operations on it.   class Cook   {     private PizzaBuilder _pizzaBuilder;      public void SetPizzaBuilder(PizzaBuilder pb)     {       _pizzaBuilder = pb;     }      public Pizza GetPizza()     {       return _pizzaBuilderGetPizza();     }      public void ConstructPizza()     {       _pizzaBuilderCreateNewPizzaProduct();       _pizzaBuilderBuildDough();       _pizzaBuilderBuildSauce();       _pizzaBuilderBuildTopping();     }   }    // Product - The final object that will be created by the Director using Builder   public class Pizza   {     public string Dough = stringEmpty;     public string Sauce = stringEmpty;     public string Topping = stringEmpty;   }    class Program   {     static void Main(string[] args)     {       PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();       Cook cook = new Cook();       cook.SetPizzaBuilder(hawaiianPizzaBuilder);       cook.ConstructPizza();       // create the product       Pizza hawaiian = cook.GetPizza();        PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();       cook.SetPizzaBuilder(spicyPizzaBuilder);       cook.ConstructPizza();       // create another product       Pizza spicy = cook.GetPizza();     }   }  } 

C++

References

  1. Gang Of Four

External links

bg: ( ) ca:Builder de:Erbauer (Entwurfsmuster) es:Builder (patr n de dise o) fr:Monteur (patron de conception) ko: it:Builder ja:Builder pl:Budowniczy (wzorzec projektowy) pt:Builder ru: ( ) uk: ( ) zh: ( )






Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article



Search for Builder pattern in Tutorials
Search for Builder pattern in Encyclopedia
Search for Builder pattern in Videos
Search for Builder pattern in Books
Search for Builder pattern in Software
Search for Builder pattern in DVDs
Search for Builder pattern in Store




Advertisement




Builder pattern in Encyclopedia
Builder_pattern top Builder_pattern

Home - Add TutorGig to Your Site - Disclaimer

©2011-2013 TutorGig.info All Rights Reserved. Privacy Statement