Builder of Design Patterns

Source: Internet
Author: User

There are 23 modes in the design pattern. This is just for one purpose: decoupling + decoupling + decoupling ... ( cohesion poly-low coupling satisfies the opening and closing principle)

Introduced:

The builder mode is a step-by-step creation of a complex object that allows the user to specify only complex objects.

Separating the construction of a complex object from its representation allows the same build process to create different representations.

Pattern structure:

First, let's look at the builder code:

 Public InterfaceBuilder {//creating a part A such as creating a car wheel    voidBuildparta (); //creating a part B such as creating a car steering wheel    voidBUILDPARTB (); //creating a part C such as creating a car engine    voidBUILDPARTC (); //returns the final assembly of the finished product (back to the last assembled car)//the assembly process for the finished product is not carried out here, but instead is transferred to the following Director class. //thus realizing the understanding of the coupling process and componentsProduct GetResult ();}

Next, let's take a look at the command director.

 Public class  Director {  private  builder Builder;     public Director (Builder builder) {  this. Builder = Builder; }  //  parts Parta partB PARTC final composition complex object  // This is the process of assembling the wheel steering wheel and the engine into a car. public      void  construct () {Builder.buildparta ();    BUILDER.BUILDPARTB ();  BUILDER.BUILDPARTC (); }}  

And then the concrete builder.

 Public classConcreteBuilderImplementsBuilder {Part parta, PartB, PARTC;  Public voidBuildparta () {//here's how to build Parta code specifically};  Public voidBUILDPARTB () {//here's how to build PARTB code specifically};  Public voidBUILDPARTC () {//here's how to build PARTB code specifically};  PublicProduct GetResult () {//return to final assembly of finished product results}; }

In this way we only need to do this in the client code:

Newnew= Builder.getresult ();

Builder is relatively simple, if you have complex objects to create when you can apply the sub-mode, you can also use the Director to control the composition of complex objects, such as mail (including attachments, topics, content ...) )

Builder of Design Patterns

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.