BuilderPattern-构建器模式

ZHIZRL / 2023-08-08 / 原文

在C#中,构造器模式(Builder Pattern)是一种创建型设计模式,用于创建一个复杂对象的过程,并将其分解为多个简单步骤进行创建。与其他创建型模式(如工厂模式)不同,构造器模式着重于对象的构建过程,而不是直接创建对象。

构造器模式通常由以下几个关键组件组成:

产品类(Product):表示构造器模式中所要创建的复杂对象的最终产物。产品类通常包含多个属性,这些属性在构造过程中逐步赋值。

构造器接口(Builder):定义了创建产品的各个步骤,包含各种构建方法来设置产品的属性。通常还要有一个获取产品的方法。

具体构造器类(Concrete Builder):实现了构造器接口,并负责实际的产品构建过程。在每个构造方法中设置产品的属性。

指导类(Director):负责组织构造器的调用顺序,指导具体构造器类如何构建产品。

class ComputerPackage
{
    public string CPU { get; set; }
    public string RAM { get; set; }
    public string Storage { get; set; }
}
// 产品类
class Computer
{
    public Computer(ComputerPackage computerPackage)
    {
        this.computerPackage = computerPackage;
    }
    public ComputerPackage computerPackage { get; set; }
    public string Brand { get; set; }
}
// 构造器接口
abstract class IComputerBuilder
{
    public abstract void SetCPU();
    public abstract void SetRAM(string ram);
    public abstract void SetStorage(string storage);
    public abstract Computer GetComputer();
}
// 具体构造器类
class LenovoComputer : IComputerBuilder
{
    private ComputerPackage computerPackage = new ComputerPackage();
    public override void SetCPU()
    {
        computerPackage.CPU = "Intel Core i7";
        Console.WriteLine("{0} build CPU {1}", this.GetType().Name, computerPackage.CPU);
    }

    public override void SetRAM(string ram)
    {
        computerPackage.RAM = ram;
        Console.WriteLine("{0} build RAM {1}", this.GetType().Name, ram);
    }

    public override void SetStorage(string storage)
    {
        computerPackage.Storage = storage;
        Console.WriteLine("{0} build RAM {1}", this.GetType().Name, storage);
    }

    public override Computer GetComputer()
    {
        SetCPU();
        Console.WriteLine("{0} 组装 {1} {2} {3}", this.GetType().Name, computerPackage.CPU, computerPackage.RAM, computerPackage.Storage);
        return new Computer(computerPackage)
        {
            Brand = "联想"
        };
    }
}
class MacComputer : IComputerBuilder
{
    private ComputerPackage computerPackage = new ComputerPackage();
    public override void SetCPU()
    {
        computerPackage.CPU = "M1";
        Console.WriteLine("{0} build CPU {1}", this.GetType().Name, computerPackage.CPU);
    }

    public override void SetRAM(string ram)
    {
        computerPackage.RAM = ram;
        Console.WriteLine("{0} build RAM {1}", this.GetType().Name, ram);
    }

    public override void SetStorage(string storage)
    {
        computerPackage.Storage = storage;
        Console.WriteLine("{0} build RAM {1}", this.GetType().Name, storage);
    }

    public override Computer GetComputer()
    {
        SetCPU();
        Console.WriteLine("{0} 组装 {1} {2} {3}", this.GetType().Name,computerPackage.CPU, computerPackage.RAM, computerPackage.Storage);
        return new Computer(computerPackage)
        {
            Brand = "苹果"
        };
    }
}
class Director
{
    private IComputerBuilder builder = null;
    public Director(IComputerBuilder builder)
    {
        this.builder = builder;
    }

    public Computer BuildComputer()
    {
        builder.SetRAM("16GB");
        builder.SetStorage("1TB SSD");
        return builder.GetComputer();
    }
}
static void Main(string[] args)
{
    IComputerBuilder builder = new LenovoComputer();
    Director director = new Director(builder);
    Computer computer = director.BuildComputer();
    Console.WriteLine($"CPU: {computer.computerPackage.CPU}");
    Console.WriteLine($"RAM: {computer.computerPackage.RAM}");
    Console.WriteLine($"Storage: {computer.computerPackage.Storage}");

    IComputerBuilder builder1 = new MacComputer();
    Director director1 = new Director(builder1);
    Computer computer1 = director1.BuildComputer();
    Console.WriteLine($"CPU: {computer1.computerPackage.CPU}");
    Console.WriteLine($"RAM: {computer1.computerPackage.RAM}");
    Console.WriteLine($"Storage: {computer1.computerPackage.Storage}");

    Console.Read();
}

 构造器模式将对象构建过程代码从上端代码中剥离,上端代码不需要知道对象构建的过程,只需要拿到想要的对象即可。