zoukankan      html  css  js  c++  java
  • 命令模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CommandSchema
    {
    class Program
    {
    static void Main(string[] args)
    {
    Barbecue boy=new Barbecue();
    
    Command bakeMuttonCommand=new BakeMuttonCommand(boy);
    Command bakeChickenWingCommand=new BakeChickenWingCommand(boy);
    
    Waiter gril=new Waiter();
    gril.SetOrder(bakeMuttonCommand);
    gril.SetOrder(bakeChickenWingCommand);
    
    gril.Notify();
    Console.ReadLine();
    }
    }
    /// <summary>
    /// 烤羊肉串的人
    /// </summary>
    class Barbecue
    {
    public void BakeMutton()
    {
    Console.WriteLine("开始烤羊肉串");
    }
    public void BakeChickenWing()
    {
    Console.WriteLine("开始烤鸡翅膀");
    }
    }
    /// <summary>
    /// 抽象命令
    /// </summary>
    abstract class Command
    {
    protected Barbecue receiver;
    
    protected Command(Barbecue receiver)
    {
    this.receiver = receiver;
    }
    
    abstract public void ExecuteCommand();
    }
    
    class BakeMuttonCommand:Command
    {
    public BakeMuttonCommand(Barbecue receiver) : base(receiver)
    {
    }
    
    public override void ExecuteCommand()
    {
    receiver.BakeMutton();
    }
    }
    
    class BakeChickenWingCommand:Command
    {
    public BakeChickenWingCommand(Barbecue receiver) : base(receiver)
    {
    }
    
    public override void ExecuteCommand()
    {
    receiver.BakeChickenWing();
    }
    }
    /// <summary>
    /// invoker
    /// </summary>
    class Waiter
    {
    private IList<Command> orders=new List<Command>();
    
    public void SetOrder(Command command)
    {
    orders.Add(command);
    Console.WriteLine("增加订单:" + command + " 时间:" + DateTime.Now);
    }
    
    public void CancelOrder(Command command)
    {
    orders.Remove(command);
    Console.WriteLine("取消订单:" + command + " 时间:" + DateTime.Now);
    }
    
    public void Notify()
    {
    foreach (Command command in orders)
    {
    command.ExecuteCommand();
    }
    }
    }
    }
  • 相关阅读:
    软件工程课程总结
    构建之法阅读笔记06
    个人工作总结11(第二阶段)
    第十六周学习进度
    大道至简阅读笔记03
    大道至简阅读笔记02
    Hadoop学习笔记(四):Yarn和MapReduce
    Hadoop学习笔记(三):java操作Hadoop
    Hadoop学习笔记(二):简单操作
    Hadoop学习笔记(一):安装与配置
  • 原文地址:https://www.cnblogs.com/i80386/p/2224810.html
Copyright © 2011-2022 走看看