Design Mode learning notes-builder Mode

Source: Internet
Author: User

Overview:

Builder mode: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

Usage:

1. When a complex object is createdAlgorithmIt should be independent from the components of the object and their assembly methods.

2. When the constructor must allow different representations of the object to be constructed.

Class diagram:

CodeStructure instance:

1. Specific product categories, that is, the products we want to manufacture

   ///     <Summary>  
/// A specific product category consists of multiple components.
/// </Summary>
Class Product
{
Ilist < String > Parts = New List < String > ();
/// <Summary>
/// Add product parts
/// </Summary>
Public Void Add ( String Part)
{
Parts. Add (part );
}
Public Void Show ()
{
Console. writeline ( " \ N product creation ---- " );
Foreach ( String Part In Parts)
{
Console. writeline (part );
}
}
}

2. the abstract class for building a product defines how to build the product and the generated results, but is not responsible for the specific construction.

   ///     <Summary>  
/// Abstract The Builder class, determine that the product consists of parta and partb, and declare a method getresult () to get the product construction result ()
/// </Summary>
Abstract Class Builder
{
Public Abstract Void Builderparta ();
Public Abstract Void Builderpartb ();
Public Abstract Product getresult ();
}

3. The specific construction class implements all the methods of the abstract class and completes the product construction and factory process according to the abstract design, but it can be based on different customer needs.

Use different accessories for product manufacturing. The following are two different product manufacturing processes that produce the same type of products.

   ///     <Summary>  
/// Specific builder class
/// </Summary>
Class Concretebuilder1: Builder
{
Private Product = New Product ();
Public Override Void Builderparta ()
{
Product. Add ( " Part " );
}
Public Override Void Builderpartb ()
{
Product. Add ( " Part B " );
}
Public Override Product getresult ()
{
Return Product;
}
}
/// <Summary>
/// Specific builder class
/// </Summary>
Class Concretebuilder2: Builder
{
Private Product = New Product ();
Public Override Void Builderparta ()
{
Product. Add ( " Part X " );
}
Public Override Void Builderpartb ()
{
Product. Add ( " Part y " );
}
Public Override Product getresult ()
{
Return Product;
}
}

4. with the product specifications, manufacturing flowchart, and manufacturing workshop, there is still a guy in charge of scheduling who operates the manufacturing machine, but the result is the product that the specific machine spit out, so this guy just closed his eyes to make it work.

/// <Summary>
///Conductor class
/// </Summary>
ClassDirector
{
Public VoidConstruct (Builder)
{
Builder. builderparta ();
Builder. builderpartb ();
}
}

5. Call the client. Let's get started.

  ///     <Summary>  
/// Test builder Mode
/// </Summary>
Static Void Testbuilder ()
{
// The conductor arrived at the scene.
Director dire = New Director ();
// Machine 1 is enabled and warmed up
Builder B1 = New Concretebuilder1 ();
// Machine 2 is enabled and warmed up
Builder B2 = New Concretebuilder2 ();

// The conductor uses concretebuilder1 to create the product.
Director. Construct (B1 );
// The product is now available from the PP of concretebuilder1.
Product p1 = B1.getresult ();
// You can see it on the desktop.
P1.show ();

// The conductor uses concretebuilder2 to create the product.
Director. Construct (B2 );
// The product is now available from the PP of concretebuilder2.
Product p2 = B2.getresult ();
// You can see it on the desktop.
P2.show ();

Console. Read ();
}

Summary:

If you are building a complex thing (object), it is better to separate the dispatcher, construction blueprint, product specifications, and construction machines. This is like a manufacturing process, but if you only need to make a handicraft, such as origami, that's all you need.

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.