C # design Pattern creation class Mode: Builder mode

Source: Internet
Author: User

Definition: (Builder Pattern)

Separating the construction of a complex object from its representation allows the same build process to create different representations.

Enlightenment:

Purchasing Manager to purchase a batch of desktop computers, in order to save the company cost, decided to assemble. But for how to assemble, the purchasing manager does not know also does not care, therefore assigns the installation business assembly. The first purchase of the computer is the assembled HP desktop,
The second purchasing manager decided to change the brand and assemble Dell's desktops. Purchasing managers do not care about how to assemble HP, Dell desktops.

Participants: Builder
Specifies an abstract interface for each part that creates a product object.
ConcreteBuilder
Implement the builder interface to construct and assemble the individual parts of the product.
Defines and clarifies the representation that it creates.
Provides an interface for retrieving products.
Director
Constructs an object that uses the builder interface.
Product
Represents a complex object that is constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process.
Contains the classes that define the components that comprise the assembly, including the interfaces that assemble the parts into the final product. Code:

First look at the product products, the realization of the computer.

/// <summary>    ///Product Category/// </summary>     Public classComputer {/// <summary>        ///Brand/// </summary>         Public stringBand {Get;Set; } /// <summary>        ///List of computer components/// </summary>        Privatelist<string> assemblyparts =Newlist<string>(); /// <summary>        ///Assembled Parts/// </summary>        /// <param name= "PartName" >part name</param>         Public voidAssemblepart (stringpartname) {             This. Assemblyparts.add (PartName); }         Public voidShowsteps () {Console.WriteLine ("start assembling the "{0}" PC:", Band); foreach(varPartinchassemblyparts) {Console.WriteLine (string. Format ("assemble "{0}";", part)); } Console.WriteLine ("assemble "{0}" computer finished! ", Band); }    }

Next look at the builder role, the computer assembler abstract class.

/// <summary>    ///the builder (simulated machine process) can also be implemented via the interface///The director does not care about the details of the specific assembly, so the specific assembly detail method is marked as protected/// </summary>     Public Abstract classBuilder {/// <summary>        ///assembling the host/// </summary>        protected Abstract voidBuildmainframepart (); /// <summary>        ///assembling the display/// </summary>        protected Abstract voidBuildscreenpart (); /// <summary>        ///assembly input Device (mouse)/// </summary>        protected Abstract voidBuildinputpart (); /// <summary>        ///Get assembled Computers///The assembly sequence is determined by the specific assembly class/// </summary>        /// <returns></returns>         Public Abstractcomputer Buildcomputer (); }

Look at the specific ConcreteBuilder role, the implementation of HP PC assembler

/// <summary>    ///HP PC Builders/// </summary>     Public classhpbulider:builder {Computer HP=NewComputer () {Band ="HP" }; protected Override voidBuildmainframepart () {hp. Assemblepart ("Host"); }        protected Override voidBuildscreenpart () {hp. Assemblepart ("Display"); }        protected Override voidBuildinputpart () {hp. Assemblepart ("Key Mouse"); }        /// <summary>        ///determine the specific assembly steps/// </summary>        /// <returns></returns>         Public OverrideComputer Buildcomputer () {Buildmainframepart ();            Buildscreenpart ();            Buildinputpart (); returnHP; }    }

Finally, just wait for the conductor director commanded, assemble the computer. Look at the conductor's realization.

/// <summary>    /// Conductor (purchasing manager)     /// </summary>     Public class Director    {        public  computer Construct (Builder Builder)        {           return  Builder. Buildcomputer ();        }    }
Advantages and Disadvantages

The builder hides the assembly process for the specific product, so changing the internal representation of a product requires just one more concrete builder, which can be a good response to changes in the demand for the product parts.

The abstract factory model solves the demand change of the "series", while the builder model solves the need for "product parts".

Application Scenarios:

The algorithm for creating complex objects should be independent of the part of the object and the way they are assembled.

The builder model focuses on the part type and assembly process (order). The most important feature is the order in which the basic methods are called.




C # design Pattern creation class Mode: Builder mode

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.