zoukankan      html  css  js  c++  java
  • 职责链模式(Chain of Responsibility)

    //注意职责链模式和状态模式的区别。

    状态模式的逻辑顺序不一定是线性的。(每个状态中只需判断即可)

    职责链模式的逻辑顺序是线性的。(每个Handler要设置Prior)

      1 /*
      2  * 高校学生请假需要符合学校规定,假如班主任可以批准1天假,系主任可以批
      3  * 准7天假,各学院院长可以批准30天,学校校长可以批准1年。应用职责链模
      4  * 式,用C#控制台应用程序实现该设计。
      5  */
      6 using System;
      7 using System.Collections.Generic;
      8 using System.Linq;
      9 using System.Text;
     10 
     11 namespace ChainOfResponsibility
     12 {
     13     abstract class Teacher
     14     {
     15         protected string name;
     16         protected Teacher prior;
     17         public Teacher(string name)
     18         {
     19             this.name = name;
     20         }
     21         public void SetPrior(Teacher prior)
     22         {
     23             this.prior = prior;
     24         }
     25         public abstract void Handle(int request);
     26     }
     27     class BanZhuRen : Teacher
     28     {
     29         public BanZhuRen(string name)
     30             : base(name)
     31         { }
     32         public override void Handle(int request)
     33         {
     34             if (request == 1)
     35             {
     36                 Console.WriteLine("{0}:学生请假" + request + "天被批准了!", name);
     37             }
     38             else
     39             {
     40                 prior.Handle(request);
     41             }
     42         }
     43     }
     44     class XiZhuRen : Teacher
     45     {
     46         public XiZhuRen(string name)
     47             : base(name)
     48         { }
     49         public override void Handle(int request)
     50         {
     51             if (request <= 7)
     52             {
     53                 Console.WriteLine("{0}:学生请假" + request + "天被批准了!", name);
     54             }
     55             else
     56             {
     57                 prior.Handle(request);
     58             }
     59         }
     60     }
     61     class YuanZhang : Teacher
     62     {
     63         public YuanZhang(string name)
     64             : base(name)
     65         { }
     66         public override void Handle(int request)
     67         {
     68             if (request <= 30)
     69             {
     70                 Console.WriteLine("{0}:学生请假" + request + "天被批准了!", name);
     71             }
     72             else
     73             {
     74                 prior.Handle(request);
     75             }
     76         }
     77     }
     78     class XiaoZhang : Teacher
     79     {
     80         public XiaoZhang(string name)
     81             : base(name)
     82         { }
     83         public override void Handle(int request)
     84         {
     85             if (request <= 365)
     86             {
     87                 Console.WriteLine("{0}:学生请假" + request + "天被批准了!", name);
     88             }
     89             else
     90             {
     91                 Console.WriteLine("{0}:请假这么多天,干脆休学一年算了!", name);
     92             }
     93         }
     94     }
     95     class Program
     96     {
     97         static void Main(string[] args)
     98         {
     99             BanZhuRen bzr = new BanZhuRen("班竹韧");
    100             XiZhuRen xzr = new XiZhuRen("西祝任");
    101             YuanZhang yz = new YuanZhang("袁章");
    102             XiaoZhang xz = new XiaoZhang("肖长");
    103 
    104             bzr.SetPrior(xzr);
    105             xzr.SetPrior(yz);
    106             yz.SetPrior(xz);
    107 
    108             string off_days;
    109             for (int i = 0; i < 5; i++)
    110             {
    111                 Console.WriteLine("输入请假天数:");
    112                 off_days = Console.ReadLine();
    113                 bzr.Handle(Convert.ToInt16(off_days));
    114             }
    115         }
    116     }
    117 }
    字节跳动内推

    找我内推: 字节跳动各种岗位
    作者: ZH奶酪(张贺)
    邮箱: cheesezh@qq.com
    出处: http://www.cnblogs.com/CheeseZH/
    * 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    1st_pandas
    8thNumpy a.copy()
    7thNumpy array合并
    6th_numpy array的合并 np.vstack np.concatenate np.newaxis
    numpy_5th array索引
    numpy_4th np.transpose(a); a.T ; a.clip(min,max) ; np.sort(a) ; np.diff() ; np.cumsum(a)
    numpy_3rd 布尔运算/乘积点积
    POJ 3270
    POJ 1721
    POJ 3128
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/2506029.html
Copyright © 2011-2022 走看看