Builder-builder Mode

Source: Internet
Author: User

Key points of the builder mode:

The builder mode is mainly used"Build a complex object in steps ". In this case, "step-by-step" is a stable multiplication, while all parts of complex objects change frequently.

The builder mode mainly dealsFrequent demand changes for "various parts of complex objects. ItsDisadvantage:It is difficult to cope with changes in the demand for "step-by-step algorithm building.

Abstract Factory ModeSolves the changes in the demand for "series objects,Builder ModeSolve the "Object part" requirement changes. Builder is usually used in combination with the composite mode.

 

Code: S

Using system;

Using system. Collections. Generic;

 

Namespace dofactory. gangoffour. Builder. Structural

{

/// <Summary>

/// Mainapp startup class for structural

/// Builder design pattern.

/// </Summary>

Public class mainapp

{

/// <Summary>

/// Entry point into console application.

/// </Summary>

Public static void main ()

{

// Create director and builders

Director dire= new director (); // instructor

 

Builder b1 = new concretebuilder1 (); // specific builder 1

Builder b2 = new concretebuilder2 (); // builder 2

 

// Construct two products

Director. Construct (B1); // The instructor directs the specific builder 1

Product p1 = b1.getresult (); // The specific builder 1 returns the product it created)

P1.show (); // display

 

Director. Construct (B2 );

Product P2 = b2.getresult ();

P2.show ();

 

// Wait for user

Console. readkey ();

}

}

 

/// <Summary>

/// The 'ctor ctor 'class

/// </Summary>

Class director // instructor class

{

// Builder uses a complex series of steps

Public void construct (Builder)

{

Builder. buildparta ();

Builder. buildpartb ();

}

}

 

/// <Summary>

// The 'builder 'abstract class

/// </Summary>

Abstract class builder // builder abstract class

{

Public abstract void buildparta ();

Public abstract void buildpartb ();

Public abstract product getresult ();

}

 

/// <Summary>

/// The 'cretebuilder1' class

/// </Summary>

Class concretebuilder1: builder // specific builder 1

{

Private product _ Product = new product ();

 

Public override void buildparta ()

{

_ Product. Add ("parta ");

}

 

Public override void buildpartb ()

{

_ Product. Add ("partb ");

}

 

Public override product getresult ()

{

Return _ product;

}

}

 

/// <Summary>

/// The 'cretebuilder2' class

/// </Summary>

Class concretebuilder2: builder // specific builder 2

{

Private product _ Product = new product ();

 

Public override void buildparta ()

{

_ Product. Add ("partx ");

}

 

Public override void buildpartb ()

{

_ Product. Add ("party ");

}

 

Public override product getresult ()

{

Return _ product;

}

}

 

/// <Summary>

/// The 'product' class

/// </Summary>

Class product // product class to be built

{

Private list <string> _ parts = new list <string> ();

 

Public void add (string part)

{

_ Parts. Add (part );

}

 

Public void show ()

{

Console. writeline ("\ nproduct parts -------");

Foreach (string part in _ parts)

Console. writeline (part );

}

}

}

Cited by: http://www.dofactory.com/Patterns/PatternBuilder.aspx

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.