C # design Pattern (5) -- Builder Pattern)

Source: Internet
Author: User

I. Introduction

In a software system, you sometimes need to create a complex object, which is composed of the molecular objects of each part through certain steps .. The following is an example of the builder mode.

Ii. detailed introduction of the builder mode implementation of the 2.1 builder Mode

In this example, the boss of the Computer City directly contacts the customer (that is, the buyer). However, the computer assembly is conducted by the boss to instruct the installer to assemble all the parts of the computer, the person responsible for product creation (here the product refers to the computer) is the installer of the Computer City. After clarifying this logical process, let's take a look at how to use code to express this logical process in real life:

                                                 Main(                                        Director director =              Builder b1 =              Builder b2 =                                           Computer computer1 =                             Computer computer2 =                                                                                                            IList<> parts =  List<>                     Add(                            Console.WriteLine(              ( part                   Console.WriteLine(+part+               Console.WriteLine(                                                                                                                                             Computer computer =                          computer.Add(                           computer.Add(                                                                   Computer computer =                          computer.Add(                           computer.Add(                             }

The code above contains detailed comments, which are not explained here. You can refer to the code and comments to compare them with real-life examples and demonstrate the running results of the code above:

2.2 builder mode definition and class diagram

After introducing the specific implementation of the builder mode, let's take a look at the specific definition of the builder mode.

Builder Pattern: separates the representation of a complex object, so that different representations can be created during the same construction process.

The builder mode separates the Construction Code from the representation code, so that the client does not have to know the details of the product composition, thus reducing the coupling between the client and a specific product, the following uses a class diagram to help you better understand the relationships between classes in the builder mode.

.

  • The builder mode is mainly used to "build a complex object by step", where "Step by Step" is a fixed combination process, the various parts of complex objects change frequently (that is to say, the internal components of the Computer change frequently. Here, the changes refer to changes such as the size of the hard disk and the CPU change from single-core to dual-core ).
  • Abstract classes are not required for products. As the final products created in the build mode may differ greatly, it is unlikely to extract an abstract product class.
  • As described in the previous article.
  • Because.
  • IV. Implementation of the builder mode in. NET

    The previous design pattern is implemented in the. NET class library. Is there any builder pattern in the. NET class library? However, the answer to the question is yes. In the. NET class library, System. Text. StringBuilder(Mscorlib. dll) is an implementation of the builder mode. However, its implementation belongs to the evolution of the builder mode. At this time, the builder mode does not have the conductor role or the abstract builder role,StringBuilderA class plays the role of a specific builder and the role of the conductor and the abstract builder. The implementation of the construction mode is as follows:

                         Product product =                                IList<> parts =  List<>          Add(  ( part  + part +        Main(= =

    The StringBuilder class assumes the specific builder role for building string objects. The ToString () method is used to return a specific product to the client (equivalent to the GetProduct method in the code above ). The Append method is used to create the product components (equivalent to the BuildPartA and BuildPartB methods in the code above), because each component in the string object is a character, therefore, the code of the role of the conductor is not required (it refers to the Construct method, which is used to call the method for creating each component to assemble the entire product ), because each component in a string object is the same and a character, the Append method also acts as the conductor's Construct method.

    V. Summary

    Here, the introduction of the Builder mode is over. The Builder mode (Builder Pattern) separates the construction of a complex object from its representation, the same build process can create different representations. The essence of the builder mode is to decouple the assembly process (encapsulated by the conductor class to achieve decoupling) from the creation of specific products, so that we do not have to worry about how each component is assembled.

    All source code in this topic: builder mode source code

    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.