zoukankan      html  css  js  c++  java
  • 建造者模式

    以下是本程序的UML图以及代码:

     

    所需class如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace M_K2
     8 {
     9   class Program
    10   {
    11     static void Main(string[] args)
    12     {
    13       KFCSetMeal KFC = new KFCSetMeal();              //client
    14       SetMealDirector KFCDirector = new SetMealDirector(KFC);
    15       Console.WriteLine("Our team name is KFC...");
    16       KFCDirector.CreateSetMeal();
    17 
    18       MCDSetMeal MCD = new MCDSetMeal();
    19       SetMealDirector MCDDirector = new SetMealDirector(MCD);
    20       Console.WriteLine("Our team name is MCD...");
    21       MCDDirector.CreateSetMeal();
    22     }
    23   }
    24 }
    25 
    26 abstract class SetMeal                            //builder
    27 {
    28   public abstract void AddBurger();
    29   public abstract void AddChips();
    30   public abstract void AddDrink();
    31 }
    32 class SetMealDirector //director
    33 {
    34   private SetMeal _builder;
    35 
    36   public SetMealDirector(SetMeal builder)
    37   {
    38     _builder = builder;
    39   }
    40 
    41   public void CreateSetMeal()
    42   {
    43     _builder.AddBurger();
    44     _builder.AddChips();
    45     _builder.AddDrink();
    46   }
    47 }
    48 
    49 class KFCSetMeal : SetMeal                           //ConcreteBuilder
    50 {
    51   public KFCSetMeal() : base(){}
    52 
    53   public override void AddBurger()
    54   {
    55     Console.WriteLine("I am Burger from KFC...");
    56   }
    57 
    58   public override void AddChips()
    59   {
    60     Console.WriteLine("I am Chips from KFC...");
    61   }
    62 
    63   public override void AddDrink()
    64   {
    65     Console.WriteLine("I am Drink from KFC...");
    66   }
    67 }
    68 
    69 class MCDSetMeal : SetMeal                            //ConcreteBuilder
    70 {
    71   public MCDSetMeal() : base() {}
    72 
    73   public override void AddBurger()
    74   {
    75     Console.WriteLine("I am Burger from MCD...");
    76   }
    77 
    78   public override void AddChips()
    79   {
    80     Console.WriteLine("I am Chips from MCD...");
    81   }
    82 
    83   public override void AddDrink()
    84   {
    85     Console.WriteLine("I am Drink from MCD...");
    86   }
    87 }

    运行结果如下:

  • 相关阅读:
    百度地图bd map使用方法
    Nodejs学习笔记2
    Nodejs学习笔记-1
    npm的安装和使用?
    如何使用jqueryUi的datepicker日历控件?
    如何 使用vim的 session和viminfo 恢复上一次工作的环境??
    Yii使用笔记 2
    关于link标签的用法, 不声明rel=stylesheet则无效? 在ff中必须声明rel属性!
    物联网产业链八大环节全景图
    一场改变你投资生涯的讨论:职业德州扑克手看交易
  • 原文地址:https://www.cnblogs.com/supercyr/p/5086763.html
Copyright © 2011-2022 走看看