首页 > 其他 > 详细

M_K2之建造者模式

时间:2015-11-28 23:16:06      阅读:573      评论:0      收藏:0      [点我收藏+]

Key point: director----builder----concrete builder

UML图如下:

技术分享

所需class如下:

技术分享

具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace M_K2
{
  class Program
  {
    static void Main(string[] args)
    {
      KFCSetMeal KFC = new KFCSetMeal();              //client
      SetMealDirector KFCDirector = new SetMealDirector(KFC);
      Console.WriteLine("Our team name is KFC...");
      KFCDirector.CreateSetMeal();

      MCDSetMeal MCD = new MCDSetMeal();
      SetMealDirector MCDDirector = new SetMealDirector(MCD);
      Console.WriteLine("Our team name is MCD...");
      MCDDirector.CreateSetMeal();
    }
  }
}

abstract class SetMeal                            //builder
{
  public abstract void AddBurger();
  public abstract void AddChips();
  public abstract void AddDrink();
}
class SetMealDirector //director
{
  private SetMeal _builder;

  public SetMealDirector(SetMeal builder)
  {
    _builder = builder;
  }

  public void CreateSetMeal()
  {
    _builder.AddBurger();
    _builder.AddChips();
    _builder.AddDrink();
  }
}

class KFCSetMeal : SetMeal                           //ConcreteBuilder
{
  public KFCSetMeal() : base(){}

  public override void AddBurger()
  {
    Console.WriteLine("I am Burger from KFC...");
  }

  public override void AddChips()
  {
    Console.WriteLine("I am Chips from KFC...");
  }

  public override void AddDrink()
  {
    Console.WriteLine("I am Drink from KFC...");
  }
}

class MCDSetMeal : SetMeal                            //ConcreteBuilder
{
  public MCDSetMeal() : base() {}

  public override void AddBurger()
  {
    Console.WriteLine("I am Burger from MCD...");
  }

  public override void AddChips()
  {
    Console.WriteLine("I am Chips from MCD...");
  }

  public override void AddDrink()
  {
    Console.WriteLine("I am Drink from MCD...");
  }
}

运行结果如下:

技术分享

应用: 将一个复杂对象的构建与它的表示分离,使同样的构建过程可以创建不同的表示 

M_K2之建造者模式

原文:http://www.cnblogs.com/Elix-kakulo/p/5003553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!