冠冕堂皇

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

代码:

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}已经签字通过\n", 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}已经签字通过\n", 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}已经签字通过\n", 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();
        }
    }
}
职责链模式样例代码

 

posted on 2014-04-25 10:17  冠冕堂皇  阅读(175)  评论(0编辑  收藏  举报