C #-Design pattern-builder mode

Source: Internet
Author: User

Builder ModeProblem Scenario

I want to buy a product, this product needs special customization, manufacturer of product details I do not want to know, just hope to get a finished product.

Summary Mode

The internal details of the manufacturing products are closed, the external is not visible, the closure details should pay attention to the application of the permission modifier, that is, the user can not call the manufacturing method, by the builder to implement the details and return the results of customer expectations,

Sample Code namespace appcui
{
//builder Abstraction
Public abstract class abstractbuilder
    {
//set to protected, manufacturing details not visible to the outside, to achieve encapsulation
protected abstract void Part1( );
protected abstract void Part2( );
//Only expose methods of acquiring products
Public abstract Product getproduct( );
    }

//Products
Public class Product
    {
Private list<string> List=new C9>list<string> ();
///set to internal so that the Add method is not visible outside the current namespace to encapsulate
internal void Add(string part)
        {
list. ADD (part);
        }
Public void Print( )
        {
Console. WriteLine ( "assembled complete" );
        }
    }

//builder Implementation
//Package manufacturing details
Public class Builder: abstractbuilder
    {
Private product product { get; Set ; }

Public Builder( )
        {
product = new product( );
        }
      
protected override void Part1( )
        {
Product. ADD ( "Part1" );
        }

protected override void Part2( )
        {
Product. ADD ( "Part2" );
        }

Public override Product getproduct( )
        {
Part1( );
Part2( );
return Product;
        }
    }

Public class programe
    {
static void Main( string[] args )
        {
abstractbuilder abstractbuilder = new Builder( ) ;
product product= abstractbuilder. getproduct ( );
product. Print ( );
        }
    }
}

  

C #-Design pattern Catalog

C #-Design pattern-builder mode

Related Article

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.