标签:profile 局限 git config 接口 rac price getc erb
创建型模式,将多个简单对象一步步构建成一个复杂的对象。
将一个复杂的构建与其表示相分离,使用同样的构建创建不同的表示。
实例应用:台式电脑的组成,是由多个零件组合成的,不同的CPU,显示器,显卡等能组合成配置完全不一样的电脑。
优点:
缺点:
代码:
/// <summary>
/// 零件
/// </summary>
public interface IIO
{
/// <summary>
/// 零件名称
/// </summary>
/// <returns></returns>
string Name();
/// <summary>
/// 零件品牌
/// </summary>
/// <returns></returns>
IVender Vender();
/// <summary>
/// 价格
/// </summary>
/// <returns></returns>
float Price();
}
/// <summary>
/// 品牌
/// </summary>
public interface IVender
{
string VenderName();
}
/// <summary>
/// 实现IVender接口的实体类 -- 英特尔品牌
/// </summary>
public class Intel : IVender
{
public string VenderName() => "英特尔";
}
/// <summary>
/// 实现IVender接口的实体类 -- 罗技品牌
/// </summary>
public class Logitech : IVender
{
public string VenderName() => "罗技";
}
/// <summary>
/// 实现IVender接口的实体类 -- 雷蛇品牌
/// </summary>
public class Razer : IVender
{
public string VenderName() => "雷蛇";
}
/// <summary>
/// 实现IIO接口的实体类 -- CPU
/// </summary>
public abstract class CPU : IIO
{
public abstract string Name();
public abstract float Price();
public IVender Vender() => new Intel();
}
/// <summary>
/// 实现IIO接口的实体类 -- 键盘
/// </summary>
public abstract class KeyBoard : IIO
{
public abstract string Name();
public abstract float Price();
public IVender Vender() => new Logitech();
}
/// <summary>
/// 实现IIO接口的实体类 -- 鼠标
/// </summary>
public abstract class Mouse : IIO
{
public abstract string Name();
public abstract float Price();
public IVender Vender() => new Razer();
}
/// <summary>
/// CPU扩展类 CPU扩展类 i5系列
/// </summary>
public class Inteli5CPU : CPU
{
public override string Name() => "Intel(R) Core(TM)i5-9400F CPU";
public override float Price() => 2999.9f;
}
/// <summary>
/// CPU扩展类 CPU扩展类 i7系列
/// </summary>
public class Inteli7CPU : CPU
{
public override string Name() => "Intel(R) Core(TM)i7-8500U CPU";
public override float Price() => 3999.0f;
}
/// <summary>
/// KeyBoard扩展类 键盘扩展类 机械键盘
/// </summary>
public class MechanicalKeyBoard : KeyBoard
{
public override string Name() => "机械键盘P230";
public override float Price() => 328.9f;
}
/// <summary>
/// KeyBoard扩展类 键盘扩展类 非机械键盘
/// </summary>
public class Non_MechanicalKeyBoard : KeyBoard
{
public override string Name() => "非机械键盘M67";
public override float Price() => 128.0f;
}
/// <summary>
/// Mouse扩展类 鼠标扩展类 机械鼠标
/// </summary>
public class MechanicalMouse : Mouse
{
public override string Name() => "机械鼠标K928";
public override float Price() => 399.0f;
}
/// <summary>
/// Mouse扩展类 鼠标扩展类 非机械鼠标
/// </summary>
public class Non_MechanicalMouse : Mouse
{
public override string Name() => "非机械鼠标S857";
public override float Price() => 99.0f;
}
/// <summary>
/// 由零件组成的复杂对象 -- 电脑
/// </summary>
public class Computer
{
private List<IIO> components = new List<IIO>();
public void AddIIO(IIO item)
{
components.Add(item);
}
public float getCost()
{
float cost = 0.0f;
foreach (var item in components)
{
cost += item.Price();
}
return cost;
}
public void ShowIIO()
{
foreach (var item in components)
{
Console.WriteLine("零件名称:{0},品牌:{1},价格:{2}", item.Name(), item.Vender().VenderName(), item.Price());
}
}
}
/// <summary>
/// 负责创建 Computer 对象
/// </summary>
public class ComputerBuilder
{
/// <summary>
/// 创建高配置电脑
/// </summary>
public static Computer HighConfiguration()
{
Computer computer = new Computer();
computer.AddIIO(new Inteli7CPU()); // i7处理器
computer.AddIIO(new MechanicalKeyBoard()); // 机械键盘
computer.AddIIO(new MechanicalMouse()); // 机械鼠标
return computer;
}
/// <summary>
/// 创建低配置电脑
/// </summary>
public static Computer LowProfile()
{
Computer computer = new Computer();
computer.AddIIO(new Inteli5CPU()); // i5处理器
computer.AddIIO(new Non_MechanicalKeyBoard()); // 非机械键盘
computer.AddIIO(new Non_MechanicalMouse()); // 非机械鼠标
return computer;
}
}
static void Main(string[] args)
{
Console.WriteLine("======建造者模式======");
// 创建一个高配置的电脑
var high = ComputerBuilder.HighConfiguration();
Console.WriteLine("高配置:");
high.ShowIIO();
Console.WriteLine("总价格:" + high.getCost());
Console.WriteLine("======================");
// 创建一个低配置的电脑
var low = ComputerBuilder.LowProfile();
Console.WriteLine("低配置:");
low.ShowIIO();
Console.WriteLine("总价格:" + low.getCost());
Console.Read();
}
组成电脑的零件当然不止这些,只是举个例子。
标签:profile 局限 git config 接口 rac price getc erb
原文地址:https://www.cnblogs.com/Canaban/p/13042673.html