标签:oar ide 思想 vat str val disk 展示 分离
建造者模式是将创建和表示进行分离,使得创建的过程中能够创建不同的表示。
据说建造者-指挥者模式已经不是常用了,这里只是学习思想
只要实现了创建和表示分离就好了,流式的配备一台电脑
将电脑的展示和电脑的组成分开
class Computer
{
private Computer(Builder builder)
{
HandDisk = builder.handDisk;
Memroy = builder.memroy;
MainBoard = builder.mainBoard;
VideoCard = builder.videoCard;
PowerSupply = builder.prowerSupply;
CUP = builder.cUP;
}
public string HandDisk { get; }
public string Memroy { get; }
public string MainBoard { get; }
public string VideoCard { get; }
public string PowerSupply { get; }
public string CUP { get; }
public class Builder
{
string HandDisk = "西部数据";
string Memroy = "金士顿";
string MainBoard = "华硕";
string VideoCard = "铭影";
string ProwerSupply = "冷静王";
string CUP = "AMD";
public string handDisk { get=> HandDisk; set => HandDisk = value; }
public string memroy { get => Memroy; set => Memroy = value; }
public string mainBoard { get => MainBoard; set => MainBoard = value; }
public string videoCard { get => VideoCard; set => VideoCard = value; }
public string prowerSupply { get => ProwerSupply; set => ProwerSupply = value; }
public string cUP { get => CUP; set => CUP = value; }
public Computer computer()
{
return new Computer(this);
}
}
电脑分成硬盘、内存、主板、显卡、电源和cup,他们展示在computer上(只有get,拼装使用构造函数里面是内部类),在这个里面有个内部类是电脑的拼装,
里面是电脑的set/get方法,并且含有默认值。这样将展示和组成分开了。
使用:
class User
{
public void BuyComputer()
{
//默认配置电脑
Computer computer = new Computer.Builder().computer();
Console.WriteLine("cup:"+computer.CPU+"...");
//自定义配置
Computer.Builder build = new Computer.Builder();
build.cPU = "intel";
Computer computer2 = build.computer();
Console.WriteLine("cup:" + computer2.CPU + "...");
}
这里的user的自定义配置有些奇怪,意思表达到就好了。。这里应该还能改进的更好
标签:oar ide 思想 vat str val disk 展示 分离
原文地址:https://www.cnblogs.com/liuyang95/p/13371056.html