职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,经这个对象连成一条链,并沿着这条连传递请求,知道处理为止

如:公司请假需逐级上批,以下以加薪和请假为例,结构图如下:

请求的类(Request)封装请求的方式,内容和数量等属性

namespace Chain_of_Responsibility_Pattern.CLASS
{
    class Request
    {
        private string resquestType;
        /// <summary>
        /// 申请类别
        /// </summary>
        public string ResquestType
        {
            get { return resquestType; }
            set { resquestType = value; }
        }
        private string resquestContent;
        /// <summary>
        /// 申请内容
        /// </summary>
        public string ResquestContent
        {
            get { return resquestContent; }
            set { resquestContent = value; }
        }
        private int resquestNumber;
        /// <summary>
        /// 申请天数
        /// </summary>
        public int ResquestNumber
        {
            get { return resquestNumber; }
            set { resquestNumber = value; }
        }
    }
}
View Code

管理者抽象类(Manager)

namespace Chain_of_Responsibility_Pattern.CLASS
{
    abstract class Manager
    {
        protected string name;
        protected Manager superior;
        public Manager(string name)
        {
            this.name = name;
        }
        /// <summary>
        /// 设置上级
        /// </summary>
        /// <param name="superior"></param>
        public void SetSuperior(Manager superior)
        {
            this.superior = superior;
        }

        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="request"></param>
        abstract public void RequestApplication(Request request);
    }
}
View Code

 经理类(CommonManager)继承于管理者抽象类(Manager)

namespace Chain_of_Responsibility_Pattern.CLASS
{
    class CommonManager:Manager
    {
        public CommonManager(string name)
            : base(name)
        { }

        public override void RequestApplication(Request request)
        {
            if (request.ResquestType == "请假" && request.ResquestNumber <= 2)
            {
                Console.WriteLine("{0},{1}数量{2}被批准!", name, request.ResquestContent, request.ResquestNumber);
            }
            else
            {
                //上级不为空
                if (superior != null)
                {
                    superior.RequestApplication(request);
                }
            }
        }
    }
}
View Code

总监类(DerectorManager)继承于管理者抽象类(Manager)

namespace Chain_of_Responsibility_Pattern.CLASS
{
    class DerectorManager:Manager
    {
        public DerectorManager(string name)
            : base(name)
        { 
        }

        public override void RequestApplication(Request request)
        {
            if (request.ResquestType == "请假" && request.ResquestNumber <= 5)
            {
                Console.WriteLine("{0},{1}数量{2}被批准!", name, request.ResquestContent, request.ResquestNumber);
            }
            else
            {
                if (superior != null)
                {
                    superior.RequestApplication(request);
                }
            }
          
        }
    }
}
View Code

总经理类(GeneralManager)继承于管理者抽象类(Manager)

namespace Chain_of_Responsibility_Pattern.CLASS
{
    class GeneralManager:Manager
    {
        public GeneralManager(string name)
            : base(name)
        { }
        public override void RequestApplication(Request request)
        {
            if (request.ResquestType == "请假")
            {
                Console.WriteLine("{0},{1}数量{2}被批准!", name, request.ResquestContent, request.ResquestNumber);
            }
            else if (request.ResquestType == "加薪" && request.ResquestNumber <= 500)
            {
                Console.WriteLine("{0},{1}数量{2}被批准!", name, request.ResquestContent, request.ResquestNumber);
            }
            else if (request.ResquestType == "加薪" && request.ResquestNumber > 500)
            {
                Console.WriteLine("{0},{1}数量{2}考虑一下!", name, request.ResquestContent, request.ResquestNumber);
            }
        }
    }
}
View Code

测试类(TestMain)

using Chain_of_Responsibility_Pattern.CLASS;
namespace Chain_of_Responsibility_Pattern
{
    class TestMain
    {
        static void Main(string[] args)
        {
            CommonManager jinli = new CommonManager("经理");
            DerectorManager zongjian = new DerectorManager("总监");
            GeneralManager zongjingli = new GeneralManager("总经理");
            
            //设置上级
            jinli.SetSuperior(zongjian);
            zongjian.SetSuperior(zongjingli);

            Request request = new Request();
            request.ResquestType = "请假";
            request.ResquestNumber = 1;
            request.ResquestContent = "张三请假";
            jinli.RequestApplication(request);

            Request request1 = new Request();
            request1.ResquestType ="请假";
            request1.ResquestNumber = 5;
            request1.ResquestContent = "李四请假";
            jinli.RequestApplication(request1);

            Request request2 = new Request();
            request2.ResquestContent = "我要加薪";
            request2.ResquestNumber = 400;
            request2.ResquestType = "加薪";
            jinli.RequestApplication(request2);

            Request request3 = new Request();
            request3.ResquestContent = "我要多加薪";
            request3.ResquestNumber = 1000;
            request3.ResquestType = "加薪";
            jinli.RequestApplication(request3);

            Console.ReadLine();
        }
    }
}
View Code

 

posted on 2013-08-19 20:30  zxd543  阅读(117)  评论(0编辑  收藏  举报