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 { }       

About The Author

Cédric Walter

I worked with various Insurances companies across Switzerland on online applications handling billion premium volumes. I love to continuously spark my creativity in many different and challenging open-source projects fueled by my great passion for innovation and blockchain technology.In my technical role as a senior software engineer and Blockchain consultant, I help to define and implement innovative solutions in the scope of both blockchain and traditional products, solutions, and services. I can support the full spectrum of software development activities, starting from analyzing ideas and business cases and up to the production deployment of the solutions.I'm the Founder and CEO of Disruptr GmbH.

Categories