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();
            }
        }
    }
    职责链模式样例代码
  • 相关阅读:
    例程 | 串口助手Comm Assist
    教程 | 蓝牙设备查找类CxBthRadio & CxBthRadioFind
    教程 | 服务端套接字类CxServerSocket
    openlayers 3加载GeoServer发布的wfs类型服务
    Geoserver端口冲突解决方案(二)
    Geoserver端口冲突解决方案
    GeoServer基础教程(四):空间数据互操作的接口规范WMS、WFS和WCS
    GeoServer基础教程(三):部署发布Shapefile地图数据
    GeoServer基础教程(二):GeoServer的Web管理界面快速入门
    GeoServer基础教程(一):环境搭建篇
  • 原文地址:https://www.cnblogs.com/gmth/p/3688075.html
Copyright © 2011-2022 走看看