The code and explanation are as follows:
Public static void BuilderPattern ()
{
// I want to buy a dell Computer
ComputerVender v = new ComputerVender ();
Computer computerObj = v. GetComputer (ComputerBrand. DELL );
Console. WriteLine (computerObj. ToString ());
// If another person wants to buy a Lenovo computer, just tell ComputerVender the brand information and do not need to modify it elsewhere
// Because each product is constructed in the same way, the specific model of each part is different, so the product construction process is consistent,
// The construction of each product component is different (for example, I want AMD CPU and you want Intel CPU). This is exactly the builder mode.
// Application: provides a consistent product construction process for the outside world, encapsulates the structural details of internal parts (the outside world does not need to care about ).
// Other application scenarios: 1. clothes matching. 2. Phone package 3. Game roles
}
/// <Summary>
/// Product
/// </Summary>
Public class Computer
{
Public string Brand {get; set ;}
Public string CPU {get; set ;}
Public string Memory {get; set ;}
Public string Disk {get; set ;}
Public string Monitor {get; set ;}
Public override string ToString ()
{
Return string. Format ("brand: {0}, CPU: {1}, memory: {2}, Hard Disk: {3}, display: {4 }",
Brand, CPU, Memory, Monitor );
}
}
/// <Summary>
/// Builder base class
/// </Summary>
Public abstract class ComputerBuilder
{
Private Computer mComputer;
Public Computer Product
{
Get
{
Return mComputer;
}
}
Public ComputerBuilder (string brand)
{
MComputer = new Computer () {Brand = brand };
}
Public abstract void AddCPU ();
Public abstract void AddMemory ();
Public abstract void AddDisk ();
Public abstract void AddMonitor ();
}
/// <Summary>
/// Specific builder class
/// </Summary>
Public class DellComputerBuilder: ComputerBuilder
{
Public DellComputerBuilder ()
: Base ("DELL ")
{
}
Public override void AddCPU ()
{
Product. CPU = "AMD 2100 ";
}
Public override void AddMemory ()
{
Product. Memory = "Kinston DDR3 ";
}
Public override void AddDisk ()
{
Product. Memory = "Seagate 500G ";
}
Public override void AddMonitor ()
{
Product. Monitor = "Samsung display ";
}
}
Public class LenovoComputerBuilder: ComputerBuilder
{
Public LenovoComputerBuilder (): base ("Lenovo "){
}
Public override void AddCPU ()
{
Product. CPU = "Intel i5 ";
}
Public override void AddMemory ()
{
Product. Memory = "Kinston DDR3 4G ";
}
Public override void AddDisk ()
{
Product. Disk = "Samsung 7500 to 500 GB ";
}
Public override void AddMonitor ()
{
Product. Monitor = "LG 22 brilliant LED display ";
}
}
Public enum ComputerBrand
{
DELL,
LENOVO
}
/// <Summary>
// Computer Retailer-Director role
/// </Summary>
Public class ComputerVender
{
Private ComputerBuilder mBuilder;
Public Computer GetComputer (ComputerBrand brand)
{
Switch (brand)
{
Case ComputerBrand. DELL:
MBuilder = new DellComputerBuilder ();
Break;
Case ComputerBrand. LENOVO:
MBuilder = new LenovoComputerBuilder ();
Break;
Default:
Throw new ArgumentException ("no computer of this brand ");
}
MBuilder. AddCPU ();
MBuilder. AddMemory ();
MBuilder. AddDisk ();
MBuilder. AddMonitor ();
Return mBuilder. Product;
}
}