Builder mode of design pattern

Source: Internet
Author: User

Related articles
Six principles of design pattern design
Seven ways to style a single case pattern of design patterns

1. Introduction to Builders ' models definition

Builder mode, which separates the construction of a complex object from its representation, so that the same build process can create different representations.

Introduction

Builder is the creation pattern of creating a complex object, decoupling the process of building a complex object from its parts, and separating the representation of the build process from the part.
For example, we want to DIY a desktop computer, we find DIY business, we can ask this computer CPU or motherboard or other components are what brand of what configuration, these parts are we can according to our needs to change, but these parts assembled into a computer process is the same, We don't need to know how these parts are assembled into a computer, we just need to provide the parts ' brand and configuration. In this case, we can use the builder model, separating the components from the assembly process, so that the construction process and components are free to expand, and the coupling between the two is minimized.

single-instance pattern structure diagram

    • Dirextor: Conductor class for unified assembly process
    • Builder: Abstract Builder class, standardize the building of products, is generally implemented by subclasses.
    • Concretebulider: An implementation class for an abstract builder class that implements all the methods defined by an abstract builder class and returns a well-formed object
    • Product: Products Category
2. Simple implementation of builder mode

Here we use DIY to assemble the computer example to achieve the builder model.

Create a Product class

I want to assemble a computer, the computer is abstracted into the computer class, it has three parts: CPU, Motherboard and memory. There are three methods for setting up the CPU, motherboard, and memory:

public      Class  Computer {private  String mcpu; private     String Mmainboard; private     String mRam; public  void  setmcpu  (String mcpu)    {this . mcpu = mcpu; } public  void      Setmmainboard  (String mmainboard) {this . Mmainboard = Mmainboard; } public  void      Setmram  (String mRam) {this . mRam = mRam; }}
Create a build of the Builder class specification product

The Merchant Assembly computer has a template for assembling methods, which is an abstract builder class that provides methods for installing the CPU, motherboard, and memory, as well as the Create method that is assembled into a computer:

publicabstractclass Builder {    publicabstractvoidbuildCpu(String cpu);    publicabstractvoidbuildMainboard(String mainboard);    publicabstractvoidbuildRam(String ram);    publicabstractcreate();}

The merchant implements the abstract builder class, the Mooncomputerbuilder class is used to assemble the computer:

 Public  class mooncomputerbuilder extends Builder {    PrivateComputer Mcomputer =NewComputer ();@Override     Public void buildcpu(String CPU)    {mcomputer.setmcpu (CPU); }@Override     Public void Buildmainboard(String mainboard)    {Mcomputer.setmmainboard (mainboard); }@Override     Public void Buildram(String RAM)    {Mcomputer.setmram (RAM); }@Override     PublicComputerCreate() {returnMcomputer; }}
unify the assembly process with the Dirextor Commander class

The merchant's conductor class is used to standardize the process specification for assembling a computer, first installing the motherboard, installing the CPU, and finally installing the memory and assembling it into a computer:

publicclass Direcror {    Builder mBuild=null;    publicDirecror(Builder build){        this.mBuild=build;    }    publicCreateComputer(String cpu,String mainboard,String ram){        //规范建造流程       this.mBuild.buildMainboard(mainboard);       this.mBuild.buildCpu(cpu);       this.mBuild.buildRam(ram);       return mBuild.create();    }}
Client invoke command class

Finally the merchant assembles the computer with the conductor class. We just need to provide the CPU, motherboard and memory we want, and we don't need to know how the business is assembled.

publicclass CreatComputer {    publicstaticvoidmain(String[]args){        Builder mBuilder=new MoonComputerBuilder();        Direcror mDirecror=new Direcror(mBuilder);        //组装电脑        mDirecror.CreateComputer("i7-6700","华擎玩家至尊","三星DDR4");    }}
3. Scenarios and pros and cons of using builder mode Usage Scenarios
    • When creating complex objects, the algorithm should be independent of the parts of the object and how they are assembled.
    • The same method, different execution order, produces different event results when.
    • Multiple parts or parts can be assembled into an object, but the resulting results are different.
    • Product classes are very complex, or the sequence of calls in a product class produces different performance.
    • When creating complex objects, the construction order between the internal components of these objects is stable, but the internal components of the objects face complex changes.
Pros and consAdvantages:
    • Using the builder mode allows the client not to know the details of the internal composition of the product.
    • The concrete builder classes are independent of each other and are easy to expand.
    • Because the concrete builders are independent, the construction process can be progressively refined without any impact on the other modules.
Disadvantages:
    • Generates redundant build objects and the Dirextor class.

Builder mode of 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.