职责链模式



模式定义

      职责链模式(Chain of Responsibility Pattern):避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。由于英文翻译的不同,职责链模式又称为责任链模式,它是一种对象行为型模式。
  
职责链模式的优点
• 降低耦合度
• 可简化对象的相互连接
• 增强给对象指派职责的灵活性
• 增加新的请求处理类很方便
职责链模式的缺点
•不能保证请求一定被接收。
•系统性能将受到一定影响,而且在进行代码调试时不太方便;可能会造成循环调用。



文章推荐(http://www.cnblogs.com/suizhouqiwei/archive/2012/11/29/2773310.html
//职责链模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Configuration;
namespace yjzcjing
{
    class Request
    {
        //申请类别
        private string requestType;
        public string RequestType
        {
            get { return requestType; }
            set { requestType = 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 Manager superior;//管理者的上级
        protected string name;//管理者
        protected 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<=5)
            {
                Console.WriteLine("{0}:{1}数量{2}被批准", name, request.RequestContent, request.Number);
            }
            else
            {
                if(superior!=null)
                {
                    superior.RequestApplications(request);
                }
            }
        }
    }
    class GeneraManager : Manager
    {
        public GeneraManager(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);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            CommonManager jingli = new CommonManager("金立");//创建管理对象
            Majordomo zongjian = new Majordomo("纵剪");
            GeneraManager zongjingli = new GeneraManager("钟经理");
            jingli.SetSuperior(zongjian);//给每个管理层设置上层领导
            zongjian.SetSuperior(zongjingli);
            Request request = new Request();//请求对象
            request.RequestType = "请假";//请求内容
            request.RequestContent = "小菜请假";
            request.Number = 10;
            jingli.RequestApplications(request);//发出请求
            Console.Read();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值