惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs

博客园 - Tristan(GuoZhijian)

Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(16) Chain of Responsibility 职责链模式
Tristan(GuoZhijian) · 2008-04-01 · via 博客园 - Tristan(GuoZhijian)

VS 2008

多个对象构成一个链式结构,客户端向链中的某个对象发出请求,被请求的对象可以处理客户代码的请求,也可以根据业务将请求向下传递,依此类推,直到请求最终被某对象处理,或抛出异常。这就是职责链模式的应用场景。

1. 模式UML图

2. 代码示例

    例子仅为演示职责链模式。
    软件从业人员,可能分SE, SSE, PL, PM等不同级别,有些工作可能按工作性质需要不同级别的人员才能处理,对于自己不能处理的工作,那么,他需要将请求传递给下家。

IWorker.cs

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

namespace DesignPattern.ChainOfResponsibility.BLL {

    
public interface IWorker {

        
void DoWork(int level);
    }

}

SE.cs

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

namespace DesignPattern.ChainOfResponsibility.BLL {
    
public class SE : IWorker {

        
private IWorker next;

        
public SE(IWorker next) {
            
this.next = next;
        }


        
IWorker Members
    }

}

SSE.cs

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

namespace DesignPattern.ChainOfResponsibility.BLL {
    
public class SSE : IWorker {

        
private IWorker next;

        
public SSE(IWorker next) {
            
this.next = next;
        }


        
IWorker Members
    }

}

PL.cs

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

namespace DesignPattern.ChainOfResponsibility.BLL {
    
public class PL : IWorker {

        
private IWorker next;

        
public PL(IWorker next) {
            
this.next = next;
        }


        
IWorker Members
    }

}

PM.cs

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

namespace DesignPattern.ChainOfResponsibility.BLL {
    
public class PM : IWorker {

        
private IWorker next;

        
public PM(IWorker next) {
            
this.next = next;
        }


        
IWorker Members
    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.ChainOfResponsibility.BLL;

namespace DesignPattern.ChainOfResponsibility {
    
class Program {
        
static void Main(string[] args) {

            IWorker worker 
= new SE(new SSE(new PL(new PM(null))));
            worker.DoWork(
3);
        }

    }

}

Output