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 }

    运行结果如下:

  • 相关阅读:
    无线路由器wds桥接技术+丢包率
    2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
    Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心
    Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列
    Codeforces Round #310 (Div. 2) A B C
    Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
    Codeforces Round #313 (Div. 2) A B C 思路 枚举 数学
    Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)
    CentOS7 PostgreSQL 安装
    数据库事务的隔离级别
  • 原文地址:https://www.cnblogs.com/supercyr/p/5086763.html
Copyright © 2011-2022 走看看