The builder pattern, in fact, is to abstract the creation process of complex objects and to produce different objects for different creation processes.
The builder hides the production process of the product, which is the process of producing the product, and if it changes the internal representation of the product, it is only possible to define a concrete builder.
The UML diagram is given below:
Here we use the code to demonstrate the use of the builder model, a very simple example, we build the building as an example, to tell the builder model more appropriate.
First we list the abstract building builders with the following code:
namespacebuilder{ Public Abstract classRoombuilder { Public Abstract voidCreateroomframe (); Public Abstract voidBuilderroom (); Public Abstract voidDecoratingroom (); Public AbstractGetroom (); }}
Roombuilder
Concrete simple and complex building builders implementation, the code is as follows:
namespacebuilder.concrete{ Public classSimpleroombuilder:roombuilder {PrivateTheNew(); Public Override voidCreateroomframe () {Guest. Frame="Simple Frame"; } Public Override voidBuilderroom () {Guest. Buildermaterial="No Using Rebar"; } Public Override voidDecoratingroom () {Guest. Decorating="Simple Decorating"; } Public OverrideGetroom () {returnthe hostel ; } }}
Simpleroombuilder
namespacebuilder.concrete{ Public classComplexroombuilder:roombuilder {PrivateTheNew(); Public Override voidCreateroomframe () {Guest. Frame="Complex Frame"; } Public Override voidBuilderroom () {Guest. Buildermaterial="Using Rebar"; } Public Override voidDecoratingroom () {Guest. Decorating="Complex Decorating"; } Public OverrideGetroom () {returnthe hostel ; } }}
Complexroombuilder
The code for the building class is as follows:
namespacebuilder{ Public classThe hostel { Public stringFrame {Set;Get; } Public stringbuildermaterial {Set;Get; } Public stringDecorating {Set;Get; } Public Override stringToString () {return This. Frame +"\ r \ n"+ This. Buildermaterial +"\ r \ n"+ This. decorating; } }}
the
The implementation of the conductor class
namespace builder{ publicclass Director { public Director (Roombuilder builder) { Builder. Createroomframe (); Builder. Builderroom (); Builder. Decoratingroom (); }}}
Director
The implementation of the Main method
namespace builder{ class program { staticvoid Main (string [] args) { new simpleroombuilder (); New Director (concrete1); = concrete1. Getroom (); Console.WriteLine (a). ToString ()); Console.readkey (); }}}
Main
The above is a simple example of the builder pattern, which is when the algorithm that creates the complex object should be independent of the part of the object and the pattern that is applied when it is assembled.
This article builder mode here, thank you for watching my blog.
Builder Mode (creator)