Builder mode of design pattern

Source: Internet
Author: User

1, the use of the scene:

If you need to separate the construction of a complex object from his presentation so that the same build process can create different representations of intent, we need you to apply a design pattern,' builder mode ', also known as the generator pattern. The builder model separates the internal representation of a product from the production process, allowing a construction process to produce a product object with different internal representations. If we use the builder model, then the user just needs to make the type that needs to be built to get them, and the concrete construction process and details don't need to know.

2. Definition :

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

3. UML

Class parsing:

Director, conductor, is to build an object that uses the builder interface
Builder is the abstract interface specified for each part of the product object that is created
ConcreteBuilder, concrete builders, implements Bulider interfaces, constructs and assembles individual components
Product, specific products.

4. Benefits of the builder model

Separating the construction code from the presentation code, as the builder hides how the product is assembled, so if you need to change the internal representation of a product, you just need to define a concrete builder.

5, the builder Basic code

The Product class----A class of products that consists of several components.

Class product{IListnew ilist<string>(); // Adding product parts  Public void Add (String part) {parts. ADD (part), s}publicvoid  Show () {consolewriteline ("\ n Product creation------------"   // enumerate all product parts foreach (String part in parts) {  Console.WriteLine-(part); }}}

The builder class--------the abstract builder class, which determines that the product consists of two parts PARTA,PARTB, and declares a method getresult to obtain the result of the product's construction.

Abstract class public abstractvoid public abstract void  publicabstract  Product getresult ();}

ConcreteBulider1 Class----------Concrete builder class

 Publicconcretebulider1:builder{PrivateProduct Product =New Product (); PublicOverridevoidBuilderparta () {//the construction of the specific two parts is part A and part BProduct. ADD ("Part A");} PublicOverridevoidBUILDERPARTB () {//the construction of the specific two parts is part A and part BProduct. ADD ("Part B");} Publicoverride Product GetResult () {returnproduct;}}

ConcreteBulider2 Class----------Concrete builder class.

 Publicconcretebulider2:builder{PrivateProduct Product =New Product (); PublicOverridevoidBuilderparta () {//construction of a specific part is part XProduct. ADD ("Part X");} PublicOverridevoidBUILDERPARTB () {//Construction of specific components yProduct. Add ("Part Y");} Publicoverride Product GetResult () {returnproduct;}}

Director class------Conductor class,

Classpublicvoid  Construct (builder builder) {// used to build the process only Builder. Builderparta (); builder. BUILDERPARTB ();}}

Client code, the customer does not need to know the specific construction process.

Static void   New New New ConcreteBuilder2 ();  // The conductor uses the ConcreteBuilder1 method to build the product. Director. Construct (B1); Product P1= B1. GetResult ();p 1.show (); Director. Construct (B2); Product P2= B1. GetResult ();p 2.show (); Console. Read ();}

The builder pattern is when the algorithm that creates the complex object should be independent of the part of the object and the mode in which they are assembled,

6. Practice Chapter

Construction Man

Builder

 Public Interface Personbuilder {void  buildhead (); void buildbody (); void Buildfoot (); Person Buildperson ();}

Concrete Construction Tools ConcreteBuilder
 Public classManbuilderImplementsPersonbuilder { person person; PublicManbuilder () { person=NewMan ();} Public voidBuildbody () {Person.setbody ("Build a man's body.");} Public voidBuildfoot () {Person.setfoot ("Build a man's foot.");} Public voidBuildhead () {Person.sethead ("Build a man's head.");} PublicPerson Buildperson () {returnPerson ;}}

Built by Director
 Public class persondirector { public person Constructperson (Personbuilder pb) {pb.buildhead ();p b.buildbody ( );p b.buildfoot (); return Pb.buildperson ();}}

Product

 Public classPerson {PrivateString head;PrivateString body;PrivateString Foot; PublicString GetHead () {returnhead;} Public voidSethead (String head) { This. Head =head;} PublicString GetBody () {returnbody;} Public voidSetbody (String body) { This. BODY =body;} PublicString Getfoot () {returnFoot;} Public voidsetfoot (String foot) { This. Foot =Foot;}} Public classMansextendsPerson {}

Test

 Public class test{publicstaticvoidnew= Pd.constructperson ( New Manbuilder ()); System.out.println (Person.getbody ()); System.out.println (Person.getfoot ()); System.out.println (Person.gethead ());}}

Results

Build men's bodies build men's feet build men's heads

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.