设计模式之责任链模式
2013-06-27 21:21
239 查看
刚学习了责任链模式,感觉还是蛮不错的,随手记录了下学习的内容。
责任链模式:责任链模式用于弱化请求发生者和请求处理者之间的关系。当多个对象都可以对请求进行处理,但不同的对象能处理的请求类型不同时,可以通过指向另一个对象的引用把这些对象连成一条责任链。当 Client 发出一个请求时,并不知道具体由哪个对象进行处理,它看到的只是一条责任链,将请求直接交给责任链,请求会在责任链中传递,直到找到一个能够进行处理的对象或者遍历结束找不到一个能够处理的对象为止。Java 语言中的异常处理机制就是责任链模式的一个典型应用例子。
下面模拟的是一个员工处理问题层次的责任链模式,不同级别的员工能处理不同级别的请求。
首先设计一个请求类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
/**
* 请求操作
* 请求分级别,不同的级别需要不同层次的去处理一个请求
* @author Share
*
*/
public class Request {
public static final int TYPE_1 = 1;
public static final int TYPE_2 = 2;
public static final int TYPE_3 = 3;
public static final int TYPE_4 = 4;
private int type;
private String msg;
public Request(int type,String msg){
this.type=type;
this.msg=msg;
}
public int getType() {
return type;
}
public String getMsg() {
return msg;
}
}
设计员工的抽象类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
/**
* 定义一个员工抽象类
* 一个员工有自己的上层,不同上层有处理问题的级别
* 如果最上层都处理不了这问题,则无人能处理这问题。
* 具体的员工类必须重写processRequest方法,才能见效
* @author Share
*
*/
public abstract class Employee {
protected Employee boss;
protected String name;
protected int requestLevel;
public Employee(Employee boss,String name){
this.boss = boss;
this.name = name;
}
public void processRequest(Request request){
if(boss!=null){
boss.processRequest(request);
}else{
System.out.println("Nobody can handle the request."+request.getMsg());
}
}
}
管理员员工类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
ublic class Manager extends Employee {
public Manager(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_1;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
主管类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
public class Director extends Employee {
public Director(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_2;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
}
CEO类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
public class CEO extends Employee {
public CEO(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_3;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
}
测试类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain; public class Main { public static void main(String[] args) { CEO ceo = new CEO(null, "Jack Ceo"); Director dir = new Director(ceo, "Sello Director"); Manager manager = new Manager(dir, "Fewen Mananger"); Request req1 = new Request(4, "我要加薪"); Request req2 = new Request(2, "我要请假"); Request req3 = new Request(1, "我要加班"); System.out.println("处理请求1"); manager.processRequest(req1); System.out.println("处理请求2"); manager.processRequest(req2); System.out.println("处理请求3"); manager.processRequest(req3); } }
打印结果:
处理请求1
Fewen Mananger say: I can't handle the request. My boss will handle it.
Sello Director say: I can't handle the request. My boss will handle it.
Jack Ceo say: I can't handle the request. My boss will handle it.
Nobody can handle the request.我要加薪
处理请求2
Fewen Mananger say: I can't handle the request. My boss will handle it.
Sello Director say: I can handle the request.我要请假
处理请求3
Fewen Mananger say: I can handle the request.我要加班
责任链可以使得系统在不影响客户端的前提下动态的安排责任链和分配责任。责任链模式中包含的角色有抽象处理者,具体处理者以及请求的发送者。责任链可以是一条直线,一个环链甚至一个树结构。它使得每一个具体的消息处理者都有可能处理消息。
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 抽象的请求处理者 * @author wly * */ public abstract class Filter { abstract String doFilter(String s); }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 具体处理者A * @author wly * */ public class FilterA extends Filter { @Override String doFilter(String s) { s = s.replace('<', '('); s = s.replace('>', ')'); return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 具体处理者B * @author wly * */ public class FilterB extends Filter { @Override String doFilter(String s) { s = s.replace('#', '$'); return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 具体处理者C * @author wly * */ public class FilterC extends Filter { @Override String doFilter(String s) { s = s.replace('W','C'); return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 责任链中的“链" * @author wly * */ public class FilterChain extends ArrayList<Filter>{ public void addFilter(Filter filter) { this.add(filter); } public String doFilter(String s) { for(Filter filter:this) { s = filter.doFilter(s); } return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 客户端类 * @author wly * */ public class Test { public static void main(String[] args){ FilterChain filterChain = new FilterChain(); filterChain.addFilter(new FilterA()); filterChain.addFilter(new FilterB()); filterChain.addFilter(new FilterC()); System.out.println(filterChain.doFilter("<今天>,W,#")); } }
输出:(今天),C,$
其实,上面的代码并不是真正的责任链,因为具体的处理者应该可以选择将请求继续往下传递还是自己直接处理掉。不过,我们可以拿Android中的Touch事件的传递来做例子。当一个view捕获到一个onTouch事件后,就可以通过onTouch的返回智决定是否继续把事件往它的父级view传递。
责任链模式:责任链模式用于弱化请求发生者和请求处理者之间的关系。当多个对象都可以对请求进行处理,但不同的对象能处理的请求类型不同时,可以通过指向另一个对象的引用把这些对象连成一条责任链。当 Client 发出一个请求时,并不知道具体由哪个对象进行处理,它看到的只是一条责任链,将请求直接交给责任链,请求会在责任链中传递,直到找到一个能够进行处理的对象或者遍历结束找不到一个能够处理的对象为止。Java 语言中的异常处理机制就是责任链模式的一个典型应用例子。
下面模拟的是一个员工处理问题层次的责任链模式,不同级别的员工能处理不同级别的请求。
首先设计一个请求类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
/**
* 请求操作
* 请求分级别,不同的级别需要不同层次的去处理一个请求
* @author Share
*
*/
public class Request {
public static final int TYPE_1 = 1;
public static final int TYPE_2 = 2;
public static final int TYPE_3 = 3;
public static final int TYPE_4 = 4;
private int type;
private String msg;
public Request(int type,String msg){
this.type=type;
this.msg=msg;
}
public int getType() {
return type;
}
public String getMsg() {
return msg;
}
}
package com.design.test.mode.responsibilityChain; /** * 请求操作 * 请求分级别,不同的级别需要不同层次的去处理一个请求 * @author Share * */ public class Request { public static final int TYPE_1 = 1; public static final int TYPE_2 = 2; public static final int TYPE_3 = 3; public static final int TYPE_4 = 4; private int type; private String msg; public Request(int type,String msg){ this.type=type; this.msg=msg; } public int getType() { return type; } public String getMsg() { return msg; } }
设计员工的抽象类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
/**
* 定义一个员工抽象类
* 一个员工有自己的上层,不同上层有处理问题的级别
* 如果最上层都处理不了这问题,则无人能处理这问题。
* 具体的员工类必须重写processRequest方法,才能见效
* @author Share
*
*/
public abstract class Employee {
protected Employee boss;
protected String name;
protected int requestLevel;
public Employee(Employee boss,String name){
this.boss = boss;
this.name = name;
}
public void processRequest(Request request){
if(boss!=null){
boss.processRequest(request);
}else{
System.out.println("Nobody can handle the request."+request.getMsg());
}
}
}
package com.design.test.mode.responsibilityChain; /** * 定义一个员工抽象类 * 一个员工有自己的上层,不同上层有处理问题的级别 * 如果最上层都处理不了这问题,则无人能处理这问题。 * 具体的员工类必须重写processRequest方法,才能见效 * @author Share * */ public abstract class Employee { protected Employee boss; protected String name; protected int requestLevel; public Employee(Employee boss,String name){ this.boss = boss; this.name = name; } public void processRequest(Request request){ if(boss!=null){ boss.processRequest(request); }else{ System.out.println("Nobody can handle the request."+request.getMsg()); } } }
管理员员工类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
ublic class Manager extends Employee {
public Manager(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_1;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
package com.design.test.mode.responsibilityChain; public class Manager extends Employee { public Manager(Employee boss, String name) { super(boss, name); requestLevel = Request.TYPE_1; } @Override public void processRequest(Request request) { if(request.getType() > requestLevel){ System.out.println(this.name+" say: I can't handle the request. My boss will handle it."); super.processRequest(request); }else{ System.out.println(this.name+" say: I can handle the request."+request.getMsg()); } } }
主管类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
public class Director extends Employee {
public Director(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_2;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
}
package com.design.test.mode.responsibilityChain; public class Director extends Employee { public Director(Employee boss, String name) { super(boss, name); requestLevel = Request.TYPE_2; } @Override public void processRequest(Request request) { if(request.getType() > requestLevel){ System.out.println(this.name+" say: I can't handle the request. My boss will handle it."); super.processRequest(request); }else{ System.out.println(this.name+" say: I can handle the request."+request.getMsg()); } } }
CEO类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain;
public class CEO extends Employee {
public CEO(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_3;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
}
package com.design.test.mode.responsibilityChain; public class CEO extends Employee { public CEO(Employee boss, String name) { super(boss, name); requestLevel = Request.TYPE_3; } @Override public void processRequest(Request request) { if(request.getType() > requestLevel){ System.out.println(this.name+" say: I can't handle the request. My boss will handle it."); super.processRequest(request); }else{ System.out.println(this.name+" say: I can handle the request."+request.getMsg()); } } }
测试类:
Java代码
![](http://zengjz88.iteye.com/images/icon_copy.gif)
![](http://zengjz88.iteye.com/images/icon_star.png)
![](http://zengjz88.iteye.com/images/spinner.gif)
package com.design.test.mode.responsibilityChain; public class Main { public static void main(String[] args) { CEO ceo = new CEO(null, "Jack Ceo"); Director dir = new Director(ceo, "Sello Director"); Manager manager = new Manager(dir, "Fewen Mananger"); Request req1 = new Request(4, "我要加薪"); Request req2 = new Request(2, "我要请假"); Request req3 = new Request(1, "我要加班"); System.out.println("处理请求1"); manager.processRequest(req1); System.out.println("处理请求2"); manager.processRequest(req2); System.out.println("处理请求3"); manager.processRequest(req3); } }
package com.design.test.mode.responsibilityChain; public class Main { public static void main(String[] args) { CEO ceo = new CEO(null, "Jack Ceo"); Director dir = new Director(ceo, "Sello Director"); Manager manager = new Manager(dir, "Fewen Mananger"); Request req1 = new Request(4, "我要加薪"); Request req2 = new Request(2, "我要请假"); Request req3 = new Request(1, "我要加班"); System.out.println("处理请求1"); manager.processRequest(req1); System.out.println("处理请求2"); manager.processRequest(req2); System.out.println("处理请求3"); manager.processRequest(req3); } }
打印结果:
处理请求1
Fewen Mananger say: I can't handle the request. My boss will handle it.
Sello Director say: I can't handle the request. My boss will handle it.
Jack Ceo say: I can't handle the request. My boss will handle it.
Nobody can handle the request.我要加薪
处理请求2
Fewen Mananger say: I can't handle the request. My boss will handle it.
Sello Director say: I can handle the request.我要请假
处理请求3
Fewen Mananger say: I can handle the request.我要加班
责任链可以使得系统在不影响客户端的前提下动态的安排责任链和分配责任。责任链模式中包含的角色有抽象处理者,具体处理者以及请求的发送者。责任链可以是一条直线,一个环链甚至一个树结构。它使得每一个具体的消息处理者都有可能处理消息。
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 抽象的请求处理者 * @author wly * */ public abstract class Filter { abstract String doFilter(String s); }
/** * 抽象的请求处理者 * @author wly * */ public abstract class Filter { abstract String doFilter(String s); }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 具体处理者A * @author wly * */ public class FilterA extends Filter { @Override String doFilter(String s) { s = s.replace('<', '('); s = s.replace('>', ')'); return s; } }
/** * 具体处理者A * @author wly * */ public class FilterA extends Filter { @Override String doFilter(String s) { s = s.replace('<', '('); s = s.replace('>', ')'); return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 具体处理者B * @author wly * */ public class FilterB extends Filter { @Override String doFilter(String s) { s = s.replace('#', '$'); return s; } }
/** * 具体处理者B * @author wly * */ public class FilterB extends Filter { @Override String doFilter(String s) { s = s.replace('#', '$'); return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 具体处理者C * @author wly * */ public class FilterC extends Filter { @Override String doFilter(String s) { s = s.replace('W','C'); return s; } }
/** * 具体处理者C * @author wly * */ public class FilterC extends Filter { @Override String doFilter(String s) { s = s.replace('W','C'); return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 责任链中的“链" * @author wly * */ public class FilterChain extends ArrayList<Filter>{ public void addFilter(Filter filter) { this.add(filter); } public String doFilter(String s) { for(Filter filter:this) { s = filter.doFilter(s); } return s; } }
/** * 责任链中的“链" * @author wly * */ public class FilterChain extends ArrayList<Filter>{ public void addFilter(Filter filter) { this.add(filter); } public String doFilter(String s) { for(Filter filter:this) { s = filter.doFilter(s); } return s; } }
Java代码
![](http://wly1028.iteye.com/images/icon_copy.gif)
![](http://wly1028.iteye.com/images/icon_star.png)
![](http://wly1028.iteye.com/images/spinner.gif)
/** * 客户端类 * @author wly * */ public class Test { public static void main(String[] args){ FilterChain filterChain = new FilterChain(); filterChain.addFilter(new FilterA()); filterChain.addFilter(new FilterB()); filterChain.addFilter(new FilterC()); System.out.println(filterChain.doFilter("<今天>,W,#")); } }
/** * 客户端类 * @author wly * */ public class Test { public static void main(String[] args){ FilterChain filterChain = new FilterChain(); filterChain.addFilter(new FilterA()); filterChain.addFilter(new FilterB()); filterChain.addFilter(new FilterC()); System.out.println(filterChain.doFilter("<今天>,W,#")); } }
输出:(今天),C,$
其实,上面的代码并不是真正的责任链,因为具体的处理者应该可以选择将请求继续往下传递还是自己直接处理掉。不过,我们可以拿Android中的Touch事件的传递来做例子。当一个view捕获到一个onTouch事件后,就可以通过onTouch的返回智决定是否继续把事件往它的父级view传递。
相关文章推荐
- 设计模式之责任链模式
- 设计模式学习之责任链模式(Chain of Responsibility,行为型模式)(22)
- 设计模式学习(十二) 责任链模式
- 从真实项目中抠出来的设计模式——第三篇:责任链模式
- 23种设计模式(20):责任链模式
- 设计模式系列之「责任链模式」
- 【java设计模式】责任链模式
- 设计模式(十)_责任链模式
- 二十四种设计模式:责任链模式(Chain of Responsibility Pattern)
- 设计模式-责任链模式
- C语言和设计模式(责任链模式)
- 设计模式-责任链模式(Chain of Responsibility Method)
- Swift设计模式之责任链模式
- python设计模式之责任链模式
- JAVA设计模式---责任链模式
- java 设计模式 -- 责任链模式
- Java设计模式之责任链模式、职责链模式
- 设计模式-责任链模式
- 设计模式 -- 责任链模式