建造者模式
在工厂模式中,关注的是谁创建了这个产品,在建造者模式中,这个产品会有多个复杂工序,例如组装电脑,提供了一个builder类来管理这个组装过程
// 抽象builder
namespace 建造者模式
{
public class Computar
{
public string CPU { get; set; }
public string MainBoard { get; set; }
}
public abstract class Builder
{
public Computar Computar { get; set; }
public abstract void BuildCpu();
public abstract void BuildMainBoard();
}
}
// 具体builder
namespace 建造者模式
{
class BuilderWang : Builder
{
public override void BuildCpu()
{
this.Computar.CPU = "Inter";
}
public override void BuildMainBoard()
{
this.Computar.MainBoard = "Huasuo";
}
}
class BuilderZhao : Builder
{
public override void BuildCpu()
{
this.Computar.CPU = "ADM";
}
public override void BuildMainBoard()
{
this.Computar.MainBoard = "ADM";
}
}
}
// 创建实例
namespace 建造者模式
{
class Program
{
static void Main(string[] args)
{
Builder wang = new BuilderWang();
wang.BuildCpu();
wang.BuildMainBoard();
Computar pc = wang.Computar;
}
}
}
标签:wang,04,void,建造,模式,Computar,class,public
From: https://www.cnblogs.com/thomerson/p/16891155.html