Design mode C # implementation (2) --- Builder

xiaoxiao2021-03-06  73

Design mode C # implementation --- Builder

Last time we learned about AbstractFactory in the creation mode. This time we will introduce one and it's more like creation mode Builder (as for details on Builder), you can refer to GOF book, not repeated here.). The purpose of Builder in Gof's book is: Separate the Construction of a complex object from it. Composition, computers, and people. So when we create this object, I think we need one step by step to create each part of the computer, first create a CPU object, Memory object, HardDisk object, and more. Builder is a mode for creating an object step by step. Memolive AbstractFactory is also an object of creating a family. Builder is also a related object, the difference between the two is very delicate in future practice.

Since the article is called the design pattern C # realment, then it is certainly less code, this time I want to say more clearly, I plan to be implemented from the following two aspects, first of all, I want to directly realize his structure, that is, we are below Those classes seen in the figure. Then I will use a specific example or a book to describe the use to deepen understanding, I hope that my description can help you learn better.

From the map we can see that there are two BuilderPart methods A, B, and a getResult method to return to the created object in our Builder interface. When we implement the interface with the Concretebuilder1 and Concretebuilder1, we joined a private object, used to return a well-established object, in the inside of this instance, has completed the initialization of the Product object. Our build is consisting of a HashTable, you can add and display your own part of your own (that is, each key / value in HashTable). I don't have not nonsense, see the implementation code below, debug in WinForm, you can refer to this series of AbstractFactory articles to find the relevant performance objects in this series (RichTextBox).

A small amount of comments in the code are for better understanding.

Using system;

Namespace builder_me {

Using system.collections;

// Specifies An Abstract Interface for Creating Parts of A Product Object.

/ / Specify an interface for a part of the object

Public interface builder {

Void BuildParta ();

Void BuildPartB ();

Product getResult ();

}

.

// defines and keeps track of the representation it creates.

// provides an interface for retrieving the product.

Public Class Concretebuilder1: Builder {

PRIVATE PRODUCT M_PRODUCT;

Public void buildparta () {

THIS.M_PRODUCT = New Product ();

THIS.M_PRODUCT.Addparts ("1", "parta");

Public void buildpartb () {

THIS.M_PRODUCT.Addparts ("2", "PartB");

}

Public product getResult () {

Return this.m_product;

}

}

Public Class Concretebuilder2: Builder {

PRIVATE PRODUCT M_PRODUCT;

Public void buildparta () {

// The method must be called first otherwise not instantiation

THIS.M_PRODUCT = New Product ();

THIS.M_PRODUCT.Addparts ("3", "Part1");

}

Public void buildpartb () {

THIS.M_PRODUCT.Addparts ("4", "Part2");

}

Public product getResult () {

Return this.m_product;

}

}

// Construct an Object Using The Builder Interface.

Public class director {

Public void construct (Builder Builder) {

// The order cannot be changed

Builder.buildparta ();

Builder.buildpartb ();

}

}

// represents the complex object under construction.concretebuilder builds

// the product's internal representation and defines the process by Which it's

// assembled.

// incrudes classes That Define the constituent parts, inclished interface

// askSEMBLING The Parts INTO The Final Result.

/ / To create a complex object, we represent it with a HashTable combination.

Public class product {

Hashtable m_parts = new hashtable ();

Public void addparts (String Partkey) {

THIS.M_PARTS.ADD (Partkey, PartValue);

}

Public string showselfparts () {

String strresult = string.empty;

INT i = 1;

Foreach (String stramp in this.m_parts.values) {

Strresult = "part" i.tostring () ": / t" strtmp "/ n";

i ;

}

Return Strresult;

}

}

}

The code segment of the client is as follows:

Director Director = New Director () and DiRector ();

Builder Builder1 = New Concretebuilder1 ();

Builder Builder2 = New Concretebuilder2 ();

Director.construct (Builder1);

Product p1 = builder1.getResult ();

This.RichtextBox1.AppendText (p1.showselfparts ()); Director.Construct (Builder2);

Product P2 = Builder2.getResult ();

This.RichtextBox1.AppendText (p2.showselfparts);

Since the example of GOF is C implementation, it is also very easy to convert to C #, I don't convert it here, interested people can convert the post.

I have limited capacity, if there is anything wrong or inaccurate, please learn your heart, my email: wu_jian830@hotmail.com.

转载请注明原文地址:https://www.9cbs.com/read-120687.html

New Post(0)