Select Page

Builder

Builder

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Source Code

 /**  * The interface Product 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(); }  /**  * The ConcreteBuilder is the product of a concrete builder.  *  */ public class ConcreteBuilder implements Product { }   /**  * The ConcreteBuilderBuilder 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;     } }   /**  * The ConcreteBuilderClient 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 class Director manages Product creation using Builder. */ public class Director {      /**      * Reference to Builder currently used      */     private Builder builder;      /**      * Create a new Director 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 interface Product defines a complex object that is  * constructed part-by-part with Builder.   */ public interface Product { }       
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Categories

0
Would love your thoughts, please comment.x
()
x