Builder mode: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
1. What is Builder?
Is the abstract interface specified by each part of a Product object.
2. What is ConcreteBuilder?
It is a specific Builder that implements the Builder interface and constructs and assembles various components.
3. What is Director?
Build an object using the Builder interface.
Builder mode code Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Builder
{
Class Program
{
Static void Main (string [] args)
{
Director dire= new director ();
Builder builder = new ConcreteBuilder ();
Director. Construct (builder );
Product p1 = builder. GetResult ();
P1.Show ();
Console. ReadLine ();
}
}
Abstract class Builder
{
Public abstract void BuilderA ();
Public abstract void BuilderB ();
Public abstract Product GetResult ();
}
Class Product
{
IList <string> parts = new List <string> ();
Public void Add (string part)
{
Parts. Add (part );
}
Public void Show ()
{
Console. WriteLine ("product creation ------");
Foreach (string part in parts)
{
Console. WriteLine (part );
}
}
}
Class ConcreteBuilder: Builder
{
Private Product product = new Product ();
Public override void BuilderA ()
{
Product. Add ("Part ");
}
Public override void BuilderB ()
{
Product. Add ("Part B ");
}
Public override Product GetResult ()
{
Return product;
}
}
Class Director
{
Public void Construct (Builder builder)
{
Builder. BuilderA ();
Builder. BuilderB ();
}
}
}
The builder mode is used when the algorithms for creating complex objects are independent of the components of the objects and their assembly methods.