Builder pattern
From Wikipedia, the free encyclopedia
The Builder Pattern is a 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 to the Composite pattern, a structure pattern.
Contents |
[edit] Class Diagram
[edit] Builder
Abstract interface for creating objects (product).
[edit] Concrete Builder
Provide implementation for Builder. Construct and assemble parts to build the objects.
[edit] 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.
[edit] Product
The final object that will be created by the Director using Builder.
[edit] 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.
[edit] Example
[edit] 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 Cook { 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 given type of pizza being constructed. */ public class BuilderExample { public static void main(String[] args) { Cook cook = new Cook(); PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder(); PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder(); cook.setPizzaBuilder(hawaiianPizzaBuilder); cook.constructPizza(); Pizza pizza = cook.getPizza(); } }
[edit] Python
# Product class Pizza: def __init__(self): self.dough = None self.sauce = None self.topping = None def set_dough(self, dough): self.dough = dough def set_sauce(self, sauce): self.sauce = sauce def set_topping(self, topping): self.topping = topping # Abstract Builder class PizzaBuilder: def __init__(self): self.pizza = None def get_pizza(self): return self.pizza def create_new_pizza_product(self): self.pizza = Pizza() # ConcreteBuilder class HawaiianPizzaBuilder(PizzaBuilder): def build_dough(self): self.pizza.set_dough("cross") def build_sauce(self): self.pizza.set_sauce("mild") def build_topping(self): self.pizza.set_topping("ham+pineapple") # Director class Cook: def __init__(self): self.pizza_builder = None def set_pizza_builder(self, pb): self.pizza_builder = pb def get_pizza(self): return self.pizza_builder.get_pizza() def construct_pizza(self): self.pizza_builder.create_new_pizza_product() self.pizza_builder.build_dough() self.pizza_builder.build_sauce() self.pizza_builder.build_topping() # A given type of pizza being constructed. if __name__ == '__main__': cook = Cook() cook.set_pizza_builder(HawaiianPizzaBuilder()) cook.construct_pizza() pizza = cook.get_pizza()
[edit] External links
- What is the difference between Factory pattern and Builder Pattern ?
- The JavaWorld article Build user interfaces without getters and setters (Allen Holub) shows the complete Java source code for a Builder.
|
|||||||||||
