
Builder

Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Source Code
/** * The interfaceProduct
defines interface to create parts of the * Product. */ public interface Builder { /** * Construct part of the complex Product. */ public void buildPart(); /** * Construct the Product. * * @return the constructed product */ public Product getProduct(); } /** * TheConcreteBuilder
is the product of a concrete builder. * */ public class ConcreteBuilder implements Product { } /** * TheConcreteBuilderBuilder
creates and assembles parts of * the Product. * */ public class ConcreteBuilderBuilder implements Builder { /** * Reference to Product being constructed */ private ConcreteBuilder concreteBuilder; /** * Construct part of the complex Product. */ public void buildPart() { // put your code here } /** * Construct the Product. * * @return the constructed product */ public Product getProduct() { return concreteBuilder; } } /** * TheConcreteBuilderClient
initialized the Director with a * Concrete Bulder to create the Product and gets result from the Builder. * */ public class ConcreteBuilderClient { /** * Use the Builder to create the Product. */ public void createProduct() { ConcreteBuilderBuilder builder = new ConcreteBuilderBuilder(); new Director(builder).construct(); Product product = builder.getProduct(); } } /** * The classDirector
manages Product creation using Builder. */ public class Director { /** * Reference to Builder currently used */ private Builder builder; /** * Create a newDirector
instance. * * @param builder the builder which will create the product */ public Director(Builder builder) { this.builder = builder; } /** * Construct the Product using the Builder. */ public void construct() { builder.buildPart(); } } /** * The interfaceProduct
defines a complex object that is * constructed part-by-part with Builder. */ public interface Product { }