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

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        /// <summary>
        /// 请求类 场景
        /// </summary>
        class Context
        {
            public Context(int day)
            {
                this.day = day;
            }
            private int day;
    
            public int Day
            {
                get { return day; }
                set { day = value; }
            }
    
        }
        /// <summary>
        /// 抽象基类
        /// </summary>
        abstract class Handler
        {
            public Handler(string name)
            {
                _name = name;
            }
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private Handler _handler;
            public Handler handler
            {
                get { return _handler; }
                set { _handler = value; }
            }
            //是否通过
            public abstract bool Pass(Context context);
        }
        /// <summary>
        /// 部门经理 2天以下的部门签字就成了
        /// 
        /// </summary>
        class Manager : Handler
        {
            public Manager(string name)
                : base(name)
            { }
            public override bool Pass(Context context)
            {
                if (context.Day <= 2)
                {
                    Console.Write("{0}已经签字通过
    ", Name);
                    return true;
                }
                return handler.Pass(context);
            }
        }
        /// <summary>
        /// 总经理 2天以上需要总经理签字
        /// </summary>
        class GeneralManager : Handler
        {
            public GeneralManager(string name)
                : base(name)
            { }
            public override bool Pass(Context context)
            {
                if (2 < context.Day && context.Day<=7)
                {
                    Console.Write("{0}已经签字通过
    ", Name);
                    return true;
                }
                return handler.Pass(context);
            }
        }
    
        /// <summary>
        /// 董事长 7天以上需要总经理签字
        /// </summary>
        class President : Handler
        {
            public President(string name)
                : base(name)
            { }
            public override bool Pass(Context context)
            {
                if (context.Day > 7)
                {
                    Console.Write("{0}已经签字通过
    ", Name);
                    return true;
                }
                return handler.Pass(context);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Context context1 = new Context(1);
                Context context2 = new Context(5);
                Context context3 = new Context(100);
                Handler manager = new Manager("部门经理");
                Handler gmanager = new GeneralManager("总经理");
                Handler president = new President("董事长");
                manager.handler = gmanager;
                gmanager.handler = president;
                manager.Pass(context1);
                manager.Pass(context2);
                manager.Pass(context3);
                Console.ReadLine();
            }
        }
    }
    职责链模式样例代码
  • 相关阅读:
    Excel技巧--按内容分列与合并
    Excel技巧--分类求和与空白批量填充
    Excel技巧--空白处补零
    Excel技巧--批量生成指定名称的文件夹
    Excel技巧--漏斗图让转化率直观明了
    Excel技巧--时尚的圆环比例图
    Excel技巧--让折线图带面积更直观生动
    Excel技巧--图表添加平均线为指标
    Excel技巧--使用温度计图让目标与实际对比更明显
    Linux之facl----设置文件访问控制列表(详解)
  • 原文地址:https://www.cnblogs.com/gmth/p/3688075.html
Copyright © 2011-2022 走看看