C# 职责链模式

职责链模式

 职责链模式(Chain of Responsibility),使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

 

结构图:

                            C# 职责链模式_第1张图片

代码;

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 职责链模式
{
    class Program
    {
        static void Main(string[] args)
        {
            CommonManager jinli = new CommonManager("金利");
            Majordomo zongjian = new Majordomo("宗剑");
            GeneralManager zhongjingli = new GeneralManager("钟精励");
            jinli.SetSuperior(zongjian);
            zongjian.SetSuperior(zhongjingli);

            Request request = new Request();
            request.RequestType = "请假";
            request.RequestContent = "小菜请假";
            request.Number = 1;
            jinli.RequestApplications(request);

            Request request2 = new Request();
            request2.RequestType = "请假";
            request2.RequestContent = "小菜请假";
            request2.Number = 4;
            jinli.RequestApplications(request2);

            Request request3 = new Request();
            request3.RequestType = "加薪";
            request3.RequestContent = "小菜请求加薪";
            request3.Number = 500;
            jinli.RequestApplications(request3);

            Request request4 = new Request();
            request4.RequestType = "加薪";
            request4.RequestContent = "小菜请求加薪";
            request4.Number = 1000;
            jinli.RequestApplications(request4);

            Console.Read();
        }
    }
    class Request
    {
        private string requestTyep;
        public string RequestType
        {
            get { return requestTyep; }
            set { requestTyep = value; }
        }
        private string requestContent;
        public string RequestContent
        {
            get { return requestContent; }
            set { requestContent = value; }
        }
        private int number;
        public int Number
        {
            get { return number; }
            set { number = value; }
        }
    }
    abstract class Manager
    {
        protected string name;
        protected Manager superior;
        public Manager(string name)
        {
            this.name = name;
        }

        public void SetSuperior(Manager superior)
        {
            this.superior = superior;
        }

        abstract public void RequestApplications(Request request);
    }
    class CommonManager : Manager
    {
        public CommonManager(string name)
            : base(name)
        { }
        public override void RequestApplications(Request request)
        {
            if (request.RequestType == "请假" && request.Number <= 2)
            {
                Console.WriteLine("{0}:{1}  数量{2} 被请批准!", name, request.RequestContent, request.Number);
            }
            else
            {
                if (superior != null)
                {
                    superior.RequestApplications(request);
                }
            }
        }
    }

    class Majordomo : Manager
    {
        public Majordomo(string name)
            : base(name)
        { }
        public override void RequestApplications(Request request)
        {
            if (request.RequestType == "请假" && request.Number <= 2)
            {
                Console.WriteLine("{0}:{1}  数量{2} 被请批准!", name, request.RequestContent, request.Number);
            }
            else
            {
                if (superior != null)
                {
                    superior.RequestApplications(request);
                }
            }
        }
    }
    class GeneralManager : Manager
    {
        public GeneralManager(string name)
            : base(name)
        { }
        public override void RequestApplications(Request request)
        {
            if (request.RequestType == "请假")
            {
                Console.WriteLine("{0}:{1}  数量{2} 被请批准!", name, request.RequestContent, request.Number);
            }
            else if (request.RequestType == "加薪" && request.Number <= 500)
            {
                Console.WriteLine("{0}:{1}  数量{2} 被请批准!", name, request.RequestContent, request.Number);
            }
            else if (request.RequestType == "加薪" && request.Number > 500)
            {
                Console.WriteLine("{0}:{1}  数量{2} 再说吧!", name, request.RequestContent, request.Number);
            }
        }
    }
}
    

解释: 

   代码通过创建一个管理者类,同时设置管理者的上级和申请请求,然后 设置三个子类 “经理”“总监”“总经理”,然后在设置一个申请类设置  : 申请类别、申请内容、申请数量三个属性。然后三个管理类就可以在自己的内部重写申请请求,各自判断是否自己可以处理,如果不能处理则通过管理者上级这个方法来来调用上级去处理。

 

总结:

  职责链在客户提交请求以后,请求顺着链传递,知道有一个可以处理对象的ConcreteHandler处理就OK了,并且链中对象自己并不知道链的结构,所以职责链可以简化对象的相互连接,他们仅需保持一个指向后继者的引用,而不需保持他所有的选接受者的引用,降低了耦合度。

你可能感兴趣的:(设计模式)