Design Mode-builder Mode

Source: Internet
Author: User

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;
}

}

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.