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