Mode roles and modes:
The builder model can separate the internal appearance of a product from the production process of the product, so that it can be a product object with different internal appearances generated during the construction process.
Using system;
Using system. collections;
Namespace csharpdesignpattern. builderpattern
{
Public class product
{
Arraylist parts = new arraylist ();
Public void add (string part)
{
Parts. Add (part );
}
}
Public interface ibuilder
{
Void buildparta ();
Void buildpartb ();
Product getresult ();
}
Public class concretebuilder1: ibuilder
{
Private product;
Public void buildparta ()
{
Product = new product ();
Product. Add ("parta ");
}
Public void buildpartb ()
{
Product. Add ("partb ");
}
Public Product getresult ()
{
Return product;
}
}
Public class concretebuilder2: ibuilder
{
Private product;
Public void buildparta ()
{
Product = new product ();
Product. Add ("partx ");
}
Public void buildpartb ()
{
Product. Add ("party ");
}
Public Product getresult ()
{
Return product;
}
}
Class director
{
Public void construct (ibuilder builder)
{
Builder. buildparta ();
Builder. buildpartb ();
}
}
Class Program
{
Static void main (string [] ARGs)
{
Director dire= new director ();
Ibuilder b1 = new concretebuilder1 ();
Ibuilder b2 = new concretebuilder2 ();
Director. Construct (B1 );
Product p1 = b1.getresult ();
Director. Construct (B2 );
Product P2 = b2.getresult ();
Console. Readline ();
}
}
}
CSHARP design mode Reading Notes (6): builder mode (learning difficulty:★★★★☆Usage frequency:★★☆☆)