Step by step. NET Design Mode Study Notes 10, Builder Mode)

Source: Internet
Author: User

Overview 

In a software system, sometimes it is faced with the creation of "a complex object", which is usually composed of sub-objects of each part using a certain algorithm; due to changes in requirements, the various parts of this complex object often face drastic changes, but the algorithms that combine them are relatively stable. How to deal with this change? How can we provide a "encapsulation mechanism" to isolate the changes of "various parts of complex objects", so as to keep the "stable Construction Algorithm" in the system from changing with demand? This is the builder mode.

This article explains the builder mode through examples in actual automobile production.

Intention

Separates a complex build from its representation so that different representations can be created during the same build process.

<Design Pattern> Builder model Diagram

General description: understanding of the Builder Mode

Builder role: provides an abstract interface to regulate the construction of each component of a product object. Generally, this interface is independent from the business logic of the application. The ConcreteBuilder role is used to directly create a product object. The method required by the specific builder class to implement this interface: one is the construction method, and the other is the result return method.

Concrete Builder role: this role is played by classes closely related to applications that create product instances under application calls. The main tasks completed by this role include:

  • Implements the interfaces provided by the Builder role and completes the process of creating product instances step by step.
  • After the construction process is complete, provide the product instance.

Ctor role: the class that acts as the role calls a specific builder role to create a product object. The director does not have the specific knowledge of the product class. What really possesses the specific knowledge of the product class is the specific builder object.

Product role: a Product is a complex object during construction.

The mentor role is a role that deals with clients. The Director role divides the client product creation request into the construction request for each part, and then delegates these requests to the specific builder role. The specific builder role is used for specific construction, but is not known to the client.


Simply put, it seems that I want a house to live in, but I don't know how to build it (simple wall building, low level) or how to design it (building several rooms, A few nice-looking, high-level), so I need to find a group of migrant workers who will build walls and have to find a designer who knows how to design them. I also want to ensure that migrant workers listen to the designer's leadership, the designers themselves do not work either. They just run the command to build a wall and a door here, so that the migrant workers can start construction. Finally, I can ask the migrant workers for a house. In this process, there is nothing for a designer, except for his design and command in his mind, so the house is also required for migrant workers. Remember!
Just like a lot of domestic enterprises that use erp, the first thing they need to do is find a software company. After finding a software company, the software company said that I only know how to write software and how to implement it, the entire erp process is unclear. Well, we have to look for a consulting company. Well, after we find Deloitte, Deloitte says, "How do I do software, I can guarantee that the software can provide you with an erp system.

Example:

The production of automobiles can be seen as a builder model. The mass production of Audi and Satana cars is as follows:

Code Design:

Create Car. cs First:

 

01 public class Car

02 {

03 private string engine;

04 public string Engine

05 {

06 get {return engine ;}

07 set {engine = value ;}

08}

09 private string gearing;

10 public string Gearing

11 {

12 get {return gearing ;}

13 set {gearing = value ;}

14}

15 private string brake;

16 public string Brake

17 {

18 get {return brake ;}

19 set {brake = value ;}

20}

21

22 public string ShowInfo ()

23 {

24 StringBuilder strBuilder = new StringBuilder ();

25 strBuilder. AppendLine ("CAR Configuration :");

26 strBuilder. AppendFormat ("Engine: {0}", Engine );

27 strBuilder. AppendFormat ("transmission speed: {0}", Gearing );

28 strBuilder. AppendFormat ("Brake system: {0}", Brake );

29 return strBuilder. ToString ();

30}

31

32}

Then create BuildCar. cs:

View sourceprint? 01 public abstract class BuildCar

02 {

03 private string name;

04 public string Name

05 {

06 get {return name ;}

07 set {name = value ;}

08}

09 private Car newCar;

10 public Car NewCar

11 {

12 get {return newCar ;}

13 set {newCar = value ;}

14}

15

16 public BuildCar ()

17 {

18 newCar = new Car ();

19}

20 public abstract void BuildEngine ();

21 public abstract void BuildGearing ();

22 public abstract void BuildBrake ();

23}

Create Audi. cs again:

View sourceprint? 01 public class Audi: BuildCar

02 {

03 public override void BuildEngine ()

04 {

05 NewCar. Engine = "V-type 12-cylinder Engine ";

06}

07

08 public override void BuildGearing ()

09 {

10 NewCar. Gearing = "four wheel drive (4WD )";

11}

12

13 public override void BuildBrake ()

14 {

15 NewCar. Brake = "Electromagnetic ";

16}

17 public Audi ()

18 {

19 Name = "Audi A8 ";

20}

21}

Create Satana. cs again:

View sourceprint? 01 publi

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.