Builder Mode (creator)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.