Builder design Pattern

Source: Internet
Author: User

Builder design PatternTable of Contents:
    • Builder mode

    • Builder Pattern Implementation

    • Sample code

    • Use

    • Applications in the JDK

Builder mode

Like factory and abstract factory design patterns, the builder design pattern is also one of the object creation types. When creating objects that are more complex, especially those that contain more properties, it is cumbersome for factory methods to create such objects, and builder is introduced to solve such problems. Factory methods There are several main types of problems that need to be addressed when creating such objects:

    • When there are many parameters, especially when these parameters are more, the client is prone to error when invoking the factory class to create objects.

    • Some of the arguments passed, some of which must be passed, and some are optional, at which point we need to set the optional parameter to NULL.

    • If the object is heavy, the process of creating it is more complex, which makes the factory class more complex.

We can provide a constructor that contains only the arguments that must be passed, and set the optional parameters in addition to the Set method that provides the optional fields. But this method, to show the invocation of the set method to ensure the consistency of the object state.

The builder design pattern allows us to create objects step-by-step and return to the final object, thus avoiding the above problems.

Builder Pattern Implementation
    • First, create a static inner class, copy all the properties of the external class into the static inner class, and in order to follow the naming specification, we name the inner class with the class name builder.

    • The static inner class contains a public constructor, all parameters that must be passed as arguments to the constructor.

    • The static inner class should provide a portal for setting optional parameters, and the set method corresponding to the parameter should be guaranteed to return the same builder object when the set method of multiple properties is called consecutively.

    • The final step is to implement a builder method in a static inner class that returns an object of the outer class. The static inner class is implemented as a parameter by implementing a private constructor in the outer class.

Sample code
public class computer {    //required parameters     private String HDD;    private String RAM;     //optional parameters    private boolean isGraphicsCardEnabled;     private boolean isbluetoothenabled;    public string  GETHDD ()  {        return hdd;    }     public string getram ()  {         return RAM;    }    public boolean  Isgraphicscardenabled ()  {        return  isgraphicscardenabled;    }    public boolean  Isbluetoothenabled ()  {        rEturn isbluetoothenabled;    }    private computer ( Computerbuilder builder)  {        this. Hdd = builder. Hdd;        this. Ram = builder. ram;        this.isgraphicscardenabled =  builder.isgraphicscardenabled;        this.isbluetoothenabled =  builder.isbluetoothenabled;    }        @ Override    public string tostring ()  {         return  "computer [hdd="  + HDD +  " ram="  + ram +   ",  isgraphicscardenabled="                 + isGraphicsCardEnabled +  ",  ISBLuetoothenabled= " + isBluetoothEnabled + " ";    }     //builder class    public static class computerbuilder {         // required parameters         private string  hdd;        private  string  ram;        // optional parameters         private boolean isGraphicsCardEnabled;         private boolean isBluetoothEnabled;         public computerbuilder (String hdd, string ram)  {             this. hdd = hdd;             this. ram = ram;        }         public computerbuilder setgraphicscardenabled (boolean isgraphicscardenabled)  {             this.isgraphicscardenabled =  isgraphicscardenabled;            return  this;        }         Public computerbuilder setbluetoothenabled (boolean isbluetoothenabled)  {             this.isBluetoothEnabled = isBluetoothEnabled;             return this;         }        public computer build () &nbsP {            return new computer (this);         }    }}
Use
Public class testbuilderpattern {    public static void main (String[] args)  {        //Using builder to  get the object in a single line of code and         //without any inconsistent state or arguments         computer comp = new computer.computerbuilder (          "500 GB",  "2 GB"). Setbluetoothenabled (True)         .setgraphicscardenabled (True). Build ();                          SYSTEM.OUT.PRINTLN (comp);         //computer [hdd=500  GB, RAM=2 GB, isgraphicscardenabled=true, isbluetoothenabled=true]     }} 
Applications in the JDK
    • Java.lang.stringbuilder#append () (unsynchronized)

    • Java.lang.stringbuffer#append () (synchronized)


Builder design Pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.