学习设计模式(创建者模式)
原文地址:http://terrylee.cnblogs.com/archive/2005/12/19/299878.html
自己写了下:
/// <summary>
/// 指导者(收银员)
/// </summary>
public class Manager
{
public void Construct(Builder builder)
{
builder.AddMeat();
builder.BuildCoke();
builder.Money();
}
/// <summary>
/// 构造者
/// </summary>
public abstract class Builder
{
/// <summary>
/// 添加肉类
/// </summary>
public abstract void AddMeat();
/// <summary>
/// 添加可乐
/// </summary>
public abstract void BuildCoke();
/// <summary>
/// 返回金钱
/// </summary>
/// <returns></returns>
public abstract int Money();
}
/// <summary>
/// 实例创建者(普通套餐)
/// </summary>
public class CommonBuilder:Builder
{
public override void AddMeat()
{
Console.WriteLine("添加一块肉");
}
public override void BuildCoke()
{
Console.WriteLine("添加一杯可乐");
}
public override int Money()
{
return 10;
}
}
/// <summary>
/// VIP套餐
/// </summary>
public class VIPBuilder:Builder
{
public override void AddMeat()
{
Console.WriteLine("添加2快肉");
}
public override void BuildCoke()
{
Console.WriteLine("添加2杯可乐");
}
public override int Money()
{
return 15;
}
}
/// <summary>
/// 创建者模式
/// </summary>
class Program
{
static void Main(string[] args)
{
//普通的套餐
Manager foodManager = new Manager();
Builder builder = new CommonBuilder();
foodManager.Construct(builder);
//vip套餐
Builder builder2 = new VIPBuilder();
foodManager.Construct(builder2);
}
}