Objective
Write to yourself-your insistence. The builder model is not complicated, but it's hard to think of an image.
Introduction-builder Mode
Definition: Separates the construction of a complex object from its representation so that the same build process can create different representations.
Realize
The builder model is mostly about dealing with complex objects, and the previous example of a skateboard shoe doesn't look right because it's not complicated. But we split a pair of shoes: soles, laces, uppers and so on. It is also not simple, the following through the builder model to achieve a shoe-making process:
The first is the builder class:
/// <summary> ///Builders ' base class/// </summary> Public Abstract class Builder {/// <summary> ///assembling the soles/// </summary> Public Abstract voidBuildtread (); /// <summary> ///assembling the upper/// </summary> Public Abstract voidBuildvamp (); /// <summary> ///Shoe Straps/// </summary> Public Abstract voidBuildshoelace (); /// <summary> ///Shoes Factory/// </summary> /// <returns></returns> Public Abstract Shoes outshoes (); }
Who is the builder in the factory, of course the worker:
// <summary> ///operator 1/// </summary> Public class Operator1 : Builder { Shoes Shoes=New Shoes(); Public Override voidbuildshoelace () {shoes. Addpart ("shoelace 1"); } Public Override voidBuildtread () {shoes. Addpart ("Sole 1"); } Public Override voidBuildvamp () {shoes. Addpart ("Upper 1"); } Public Override Shoes outshoes () {returnshoes; } } /// <summary> ///operator 2/// </summary> Public class Operator2 : Builder { Shoes Shoes=New Shoes(); Public Override voidbuildshoelace () {shoes. Addpart ("shoelace 2"); } Public Override voidBuildtread () {shoes. Addpart ("Sole 2"); } Public Override voidBuildvamp () {shoes. Addpart ("Upper 2"); } Public Override Shoes outshoes () {returnshoes; } }
or the product-shoes:
// <summary> ///Shoes/// </summary> Public class Shoes { PublicShoes () {Parts=New List<string>(); } List <string> Parts {Get;Set; } Public voidAddpart (stringPart ) {Parts.add (part); } Public voidShowparts () { for(inti =0, C = Parts.count;i < C; i++) {Console.WriteLine ("The order in which my skateboard shoes are built is _{0}:{1}", I,parts[i]); } } }
If it were your workers, would you work consciously? (I won't, hehe ...) So need the boss to command):
Public class Boss { public Shoes buidshoes (builder Builder) { Builder. Buildtread (); Builder. Buildvamp (); Builder. Buildshoelace (); return Builder. Outshoes (); } }
Here's the call:
namespacedesignpattern{classProgram {Static voidMain (string[] args) {Boss Boss=NewBoss (); Builder Builder1=NewOperator1 (); Shoes shoes1=boss. Buidshoes (Builder1); Shoes1. Showparts (); Console.WriteLine ("-----------------Gorgeous split-line-----------------"); Builder Builder2=NewOperator2 (); Shoes Shoes2=boss. Buidshoes (BUILDER2); Shoes2. Showparts (); Console.read (); } }}
Operation Result:
Welcome to criticize correct, reprint please indicate source http://www.cnblogs.com/xinwang/p/6380564.html
C # Design Pattern consolidation Notes-builder mode