C # Design Pattern Series Tutorials-builder Mode _c# tutorial

Source: Internet
Author: User

1. Overview

Separating the construction of a complex object from its representation allows the same build process to create different representations, which are called builder patterns.

2. Role in the builder model

2.1 Builder (Builder): Specifies an abstract interface for each part that creates a product object.
2.2 Concrete Builder (ConcreteBuilder): Implements the interface of the builder to construct and assemble the parts of the product, define and clarify the representations it creates, and provide an interface to retrieve the product.
2.3 Conductor (Director): Directs and constructs an object that uses the builder interface.
2.4 (Product): Represents a constructed complex object. ConcreteBuilder creates an internal representation of the product and defines its assembly process, containing the classes that define the components, including the interfaces that assemble the parts into the final product.

3. Example: Create a person, this person can have different characteristics, can be fat, can be thin, can be tall, can also be short.

3.1 Below is the class diagram for this example, and the interpretation of class diagrams.

3.2 Code Implementation and interpretation:

  The product to be built public class person {public string head {get; set;}
    public string Body {get; set;}
    public string Arm {get; set;}
  public string Leg {get; set;} }//define the Creator interface, the implementing person must implement all the abstract methods defined in the interface to prevent the implementation from inadvertently omitting the creation of a part of the public abstract class Builder {protected person {g Et Set
    Public Builder () {person = new person ();
    //Construction Head public abstract void Buildhead ();
    Construction of the body public abstract void buildbody ();
    Build arm public abstract void buildarm ();

    Build legs public abstract void buildleg ();
    Returns the generated object, which is a concrete method that each subclass can use to return an object that has already been created successfully by public person Getperson () {returns person;
      }//Builder's concrete implementation, here is to build a skinny public class Thinpersonbuilder:builder {public Thinpersonbuilder () {
    person = new person ();
    public override void Buildhead () {person.head = "thin Head";
    public override void Buildbody () {person.body = "thin Body"; }

    public override void Buildarm () {person.arm = "Skinny's arm";
    public override void Buildleg () {Person.leg = "thin legs"; 
      }//Builder's concrete implementation, here is to build out a fat public class Fatpersonbuilder:builder {public override void Buildhead () {
    Person.head = "Fat Man's Head";
    public override void Buildbody () {person.body = "Fatty's body";
    public override void Buildarm () {person.head = "fat Man's arm";
    public override void Buildleg () {person.head = "Fatty's leg";
    }//Builder Mode command public class Persondirector {Builder Builder;
    Public Persondirictor (Builder personbuilder) {Builder = Personbuilder; //directs the process of creating a person, and returns the creation of a successful product public persons Buildperson () {Builder.
      Buildhead (); Builder.
      Buildbody (); Builder.
      Buildarm (); Builder.

      Buildleg (); Return builder.
    Getperson ();

 }
  }

4. Model Summary

4.1 Advantages

4.1.1 users only need to specify the type they want to build to get them, and the specific construction process and details do not need to know.
4.1.2 The construction code is separated from the presentation, if you want to change the internal representation of a product, just redefine a new concrete builder.
The 4.1.3 construction process is controlled by the conductor, the details of which are controlled by an abstract class, and a step is not omitted for the specific class that implements the construction details.

4.2 Disadvantages

The above example shows that now I want to add a detail to the product, such as adding the process of creating the foot, to see what classes we need to change. Buider,fatpersonbuilder,thinpersonbuilder (even more, if you realize the Tallpersonbuilder,shortpersonbuilder etc), PersonDirector, We are open to changes.

4.3 Practical range
4.3.1 the algorithm for creating complex objects should be independent of the components of the object and the way they are assembled.

4.3.2 when the parts of a complex object are relatively stable and are not changed

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.