Author: cuike519 column http://blog.9cbs.net/cuike519/ we will create a schema to introduce and it's more like
Builder
(As for about
Builder
Details You can refer to
Gof
The book is not repeated here. ). in
Gof
Book
Builder
The purpose is this:
Separate The Construction of a Complex Object from Its Representation So That The Same Construction Process Create Different Representation.
It is inevitable that complex objects such as complicated objects such as: The composition of the computer, and people. So when we create this object, I think we need to create every part of the computer, first create
CPU
Object,
Memory
Object,
Harddisk
Object, etc.
Builder
That is to use to create each part of the object step by step. Recall
AbstractFactory
Also create a family-related object,
Builder
It is also some related objects, and the difference between the two is very delicate in the 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 ();
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.