C#面向对象设计模式13:责任链模式(ChainofResponsibility)

责任链模式,先占位,由于文章太耗时,后面补上。

using System;
using System.Collections.Generic;
using System.Linq;

namespace SimpleFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            //前台服务员收到客人订单,录入系统下订单。
            控制系统 controller = new 控制系统();
            controller.客人下单("西红柿炒蛋");
            controller.客人下单("鱼香肉丝");
            controller.客人下单("酸菜鱼");
            controller.客人下单("醋溜土豆丝");
            controller.客人下单("麻婆豆腐");
        }
    }

    public class 控制系统
    {
        public void 客人下单(string 菜)
        {
            厨师 cook1 = new 刘师傅();
            厨师 cook2 = new 孙师傅();
            厨师 cook3 = new 马师傅();
            厨师 cook4 = new 谢师傅();
            cook1.下一位师傅(cook2);
            cook2.下一位师傅(cook3);
            cook3.下一位师傅(cook4);

            cook1.制作(菜);
        }
    }

    public abstract class 厨师
    {
        protected abstract string 菜 { get; }

        private 厨师 _下一位师傅;
        public void 下一位师傅(厨师 cook)
        {
            this._下一位师傅 = cook;
        }

        public void 制作(string 菜名)
        {
            if (菜 == 菜名)
            {
                Console.WriteLine($"{this.GetType().Name}制作出了一道{菜},好香啊");
            }
            else
            {
                if (this._下一位师傅 == null)
                {
                    Console.WriteLine($"很遗憾,没有师傅会做这道菜:{菜名},看来又要招人了");
                }
                else
                {
                    this._下一位师傅.制作(菜名);
                }
            }
        }
    }

    public class 刘师傅 : 厨师
    {
        protected override string 菜 => "鱼香肉丝";
    }

    public class 孙师傅 : 厨师
    {
        protected override string 菜 => "西红柿炒蛋";
    }

    public class 马师傅 : 厨师
    {
        protected override string 菜 => "酸菜鱼";
    }

    public class 谢师傅 : 厨师
    {
        protected override string 菜 => "醋溜土豆丝";
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值