"Java class design pattern-02" builder mode (builder pattern)

Source: Internet
Author: User

Reference 1:http://en.wikipedia.org/wiki/builder_pattern

Reference 2:http://www.cnblogs.com/devinzhang/archive/2012/01/06/2314670.html

Builder mode executes and returns the result of a constructed object to a large number of constructors, step-by-step. The intent of the Builder design pattern is toseparate the construction of a complex object from its representation. B Y Doingso the same construction process can create different representations. The structure is as follows:


Builder : An interface that creates a large number of objects.

ConcreteBuilder : Implements the Builder class that returns the final Product .

Director : Get Builder and executes all constructs.

The following is illustrated in reference 2 as An example:

public interface Builder {void Buildparta ();    void Buildpartb ();  void Buildpartc ();       ...  Product GetResult ();     }    //Concrete Construction Tools public class ConcreteBuilder implements Builder {part parta, PartB, PARTC;    public void Buildparta () {//Here is the code for how to build Parta};     public void Buildpartb () {//Here is the code for how to build PARTB};  public void Buildpartc () {//Here is how to build PARTB code}; public Product GetResult () {//return final assembly of finished product};      }    //Builder public class Director {private builder builder;    Public Director (Builder builder) {this.builder = builder;      } public void construct () {Builder.buildparta ();      BUILDER.BUILDPARTB (); BUILDER.BUILDPARTC ();           ......    }} The instantiation of product depends on the various instantiation public of part interface product {}public Interfacepart {}


*******************************************************************************

Here's how to call Builder:

ConcreteBuilder builder = Newconcretebuilder () Director Director = new Director (builder);   Director.construct (); Product Product = Builder.getresult ();


*******************************************************************************

The Director is responsible for performing the construction of the ConcreteBuilder, the advantage is the separation of the construction process. It is also possible for ConcreteBuilder to perform this build process by itself, removing the director, which is a simplified version of the builder pattern.

"Java class design pattern-02" builder mode (builder 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.