http://www.codeproject.com/Articles/42415/Builder-Design-Pattern
In Elizabeth's day care center, the teacher helps the kids to build all kinds of toys to develop their creative skills. One of Elizabeth's favorite activities is to make animals with play-dough.
在伊丽莎白所在的托儿所,老师帮助孩子们,构造各种各样玩具来开发孩子们的创造能力。伊丽莎白最喜欢的一个活动是用橡皮泥来做动物。
A set of molds is the tool that Elizabeth always uses to create her favorite cool animals.
一套模具,是伊丽莎白经常用来创造她最喜欢最酷的动物的工具。
One mold tool set includes five parts, including the head, body, arm, leg, and tail. Whenever Elizabeth wants to build an animal, she will use one set of each tools to make a head, body , leg, arm, and tail for that animal, and then assembles them with glue to build an animal.
一套模具包含五个部分,包括头、身体、手臂、腿、尾巴。无论何时,当伊丽莎白想要构造一个动物的时候,她就会使用那个动物模具中分别来做 头、身体、腿、手臂以及尾巴。最后用胶水把它们粘起来,组成一个动物。
There are many types of animal mold tool sets that the kids can choose from.
这里有许多类型的动物模具让孩子们可以进行选择。
For example: if Elizabeth wants to make a monkey, then she will pick the set of monkey molds to start.
例如:如果伊丽莎白想要做一个猴子,那么她会挑选出猴子模具做一下步骤:
Once all the five parts are finished, then Elizabeth will glue them all together and decorate it to have a monkey done as a finished product.
一旦五个部分完成了,那么伊丽莎白就会用胶水把它们粘起来,最后装饰一下它,就可以得到一个完成的作品,一个猴子。
Most likely, she will give it to me or her mom as a gift when we pick her (she will not give us the monkey if the monkey is not decorated, since it will not be looking good at all then).
在我们去托儿所接她的时候,大多数情况下,她会把这个猴子作为礼物送给我或者她的妈妈。(在猴子没有装饰之前,它不会把猴子给我们,因为没有装饰前,它一点都不好看)
When she wants to make a kitten, she follows the same steps with the set of Kitten molds.
当她想要做一个小猫的时候,她使用小猫的模具,按照相同的步骤来处理。
In the above scenario, an Builder Design Pattern is perfectly used by Elizabeth in her daily fun activities.
在上述情景中,伊丽莎白在她有趣的日常活动中完美地使用了创建者模式
The Builder Design Pattern helps us to slice the operations of building an object.
创建者模式帮助我们分解创建一个对象的操作
It also enforces a process to create an object as a finished product.
它会确保创建一个对象作为完成的产品是按照一个程序来执行的。
That means an object has to be massaged by some instructed steps before it is ready and can be used by others.
instructed 受教育的;得到指示的
The massage process could apply any restrictions or business rules for a complete building procedure (or a procedure we follow to make an object that is considered as a ready to use object).
restriction 限制;约束;束缚
procedure 程序,手续;步骤
For instance, to compose an email, you can't leave the To and Subject fields blank before you can send it.
compose 构成;写作;使平静;排…的版
In other words, an email object will be considered as an uncompleted email object (a common business rule for email) if those two fields are not filled.
It has to be built (filled) before we can send it out.
This article introduces an implementation of how we use the Builder Design Pattern for Elizabeth's fun activity.
The AnimalBuilder
class is a base abstract class. It contains all the building methods to construct a general product (Animal
).A public Animal
object is also declared here to implement the concept (GetResult
) of returning a ready to use object. Well, it can also be a private product object; in that case, a GetResult
method has to be introduced to return a ready to use product.
public abstract class AnimalBuilder { public Animal animal; public abstract void BulidHead(); public abstract void BulidBody(); public abstract void BulidArm(); public abstract void BulidLeg(); public abstract void BulidTail(); }
MonkeyBuilder
The MonkeyBuilder
class is a child class of AnimalBuilder
. It provides the details of each building method for a Monkey
. In this class, we apply all the rules to make our product (Monkey
) a ready to use object (decorated monkey). In the constructor, we also initialize the Animal
object (aAnimal
) to be a Monkey
object.
class MonkeyBuilder:AnimalBuilder { public MonkeyBuilder() { animal = new Monkey(); } public override void BulidHead() { animal.Head = "Moneky's Head has been built"; } public override void BulidBody() { animal.Body = "Moneky's Body has been built"; } public override void BulidArm() { animal.Arm = "Moneky's Arm has been built"; } public override void BulidLeg() { animal.Leg = "Moneky's Leg has been built"; } public override void BulidTail() { animal.Tail = "Moneky's Tail has been built"; } }
/// <summary> /// Same as MonkeyBuilder, the KittenBuilder class will implement the details for building a Kitten object. /// </summary> class KittenBuilder : AnimalBuilder { public KittenBuilder() { animal = new Kitten(); } public override void BulidHead() { animal.Head = "Kitten's Head has been built"; } public override void BulidBody() { animal.Body = "Kitten's Body has been built"; } public override void BulidArm() { animal.Arm = "Kitten's Arm has been built"; } public override void BulidLeg() { animal.Leg = "Kitten's Leg has been built"; } public override void BulidTail() { animal.Tail = "Kitten's Tail has been built"; } }
/// <summary> /// The Animal class is an abstract base class that holds the basic information for a product. It helps us build multiple products. /// </summary> public abstract class Animal { public string Head { get; set; } public string Body { get; set; } public string Arm { get; set; } public string Leg { get; set; } public string Tail { get; set; } //helper method for demo the Polymorphism, so we can //easily tell what type object it is from client. public abstract void Eat(); //helper method for demo the result from client public void ShowMe() { Console.WriteLine(Head); Console.WriteLine(Body); Console.WriteLine(Arm); Console.WriteLine(Leg); Console.WriteLine(Tail); Eat(); } }
/// <summary> /// The Monkey class is a concrete product class that will be built from a MonkeyBuilder. /// </summary> public class Monkey : Animal { //helper method to show monkey's property for demo purpose public override void Eat() { Console.WriteLine("Since I am Monkey, I like to eat banana"); } }
/// <summary> /// Same as the Monkey class, a concrete product class that will be built from a Kittenbuilder. /// </summary> public class Kitten : Animal { public override void Eat() { Console.WriteLine("Since I am Kitten, I like to eat kitten food"); } }
class Kid { public string Name { get; set; } //construct process to build an animal object, //after this process completed, a object //will be consider as a ready to use object. public void MakeAnimal(AnimalBuilder animalBuilder) { animalBuilder.BulidHead(); animalBuilder.BulidBody(); animalBuilder.BulidArm(); animalBuilder.BulidLeg(); animalBuilder.BulidTail(); } }
/// <summary> /// From the client side, I create a Kid (constructor object) named Elizabeth. /// Elizabeth will use the monkey mold tool set to make a monkey, and she also uses the kitten mold toolkit to make a kitten. /// From the client, you will see I can directly use builderA.animal as a ready to use object after it's been built (because it has the public property in the base AnimalBuilder class). /// It might be a private field which has a public method to access it as well. /// </summary> class Program { static void Main(string[] args) { AnimalBuilder animalBuilder; Kid kid = new Kid { Name = "Elizabeth" }; Console.WriteLine("{0} start making a monkey", kid.Name); animalBuilder = new MonkeyBuilder(); kid.MakeAnimal(animalBuilder); animalBuilder.animal.ShowMe(); Console.WriteLine(); Console.WriteLine("{0} start making a kitten", kid.Name); animalBuilder = new KittenBuilder(); kid.MakeAnimal(animalBuilder); animalBuilder.animal.ShowMe(); Console.ReadKey(); } }
once we start our client app, you will see a Monkey
and a Kitten
are both created. All the corresponding fields are completed to get ready to use objects. Cool!
In this article, I demonstrated how we can use the Builder Pattern to achieve an implementation to construct objects as ready to use based on some requirements.
I have also written another article for the Visitor Design Pattern for Elizabeth's day care center: Visitor_Design_Pattern.aspx.