责任链模式(Chain of Responsibility)
来源:互联网 发布:热传导模拟软件 编辑:程序博客网 时间:2024/05/01 10:35
职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。
针对如下需求,有一个字符串过滤链,需要过滤字符串中的某些字符,如要把“<>”替换为“{}”,“天朝”替换为“中国”等操作。可以先定义一个接口:
public interface Filter {String doFilter(String str);}然后针对需求,实现不同的实现类:
public class AFilter implements Filter {@Overridepublic String doFilter(String str) {String r = str.replace('<', '{') .replace('>', '}');return r;}}
public class BFilter implements Filter {@Overridepublic String doFilter(String str) {String r = str.replace("天朝", "中国");return r;}}定义链条:
public class FilterChain implements Filter {List<Filter> filters = new ArrayList<Filter>();public FilterChain addFilter(Filter f) {this.filters.add(f);return this;}public String doFilter(String str) {String r = str;for(Filter f: filters) {r = f.doFilter(r);}return r;}}
但是此上的职责链灵活性并不够,可以针对多个处理类对象,每个对象持有对下一个对象的引用,形成一条链,请求在这条链上传递,直到某一对象决定处理该请求。
如下的例子:public interface IHandle {public abstract void operator(String mesg);public abstract void addChain(IHandle h);public abstract IHandle getChain();}public class Manager implements IHandle {private IHandle nextChain = null;private String responsibility = "Get Project";@Overridepublic void operator(String mesg) {if (mesg.equals(responsibility)) {System.out.println("A manager --> Get a Project");} else {if (nextChain != null) {nextChain.operator(mesg);}}}@Overridepublic void addChain(IHandle h) {nextChain = h;}@Overridepublic IHandle getChain() {return nextChain;}}public class Programmer implements IHandle {private IHandle nextChain = null;private String responsibility = "Coding";@Overridepublic void operator(String mesg) {if (mesg.equals(responsibility)) {System.out.println("A Programmer --> Coding");} else {if (nextChain != null) {nextChain.operator(mesg);}}}@Overridepublic void addChain(IHandle h) {nextChain = h;}@Overridepublic IHandle getChain() {return nextChain;}}public class ProjectManager implements IHandle {private IHandle nextChain = null;private String responsibility = "Design";@Overridepublic void operator(String mesg) {if (mesg.equals(responsibility)) {System.out.println("A PM --> Design");} else {if (nextChain != null) {nextChain.operator(mesg);}}}@Overridepublic void addChain(IHandle h) {nextChain = h;}@Overridepublic IHandle getChain() {return nextChain;}}public class Test {public static void main(String[] args) {Manager manager = new Manager();ProjectManager pm = new ProjectManager();Programmer programmer = new Programmer();manager.addChain(pm);pm.addChain(programmer);manager.operator("Get Project");manager.operator("Design");manager.operator("Coding");}}
或则采取如下的结构:
public abstract class Chain {protected Chain nextChain;public abstract void doChain(Object vo);public void setChain(Chain nextChain){this.nextChain = nextChain;}protected void sendNextChain(Object vo){if(nextChain !=null){nextChain.doChain(vo);}}}
但是,如果不是单向链呢?比如数据从A链到B链处理后,希望处理的结果由B链经A连返回出来(类似计算机网络的五层结构的数据流向)。例子如下:
public class Request {String requestStr;public String getRequestStr() {return requestStr;}public void setRequestStr(String requestStr) {this.requestStr = requestStr;}}public class Response {String responseStr;public String getResponseStr() {return responseStr;}public void setResponseStr(String responseStr) {this.responseStr = responseStr;}}public interface Filter {void doFilter(Request request, Response response, FilterChain chain);}public class AFilter implements Filter {@Overridepublic void doFilter(Request request, Response response, FilterChain chain) {request.requestStr = request.requestStr.replace('<', '{') .replace('>', '}');chain.doFilter(request, response, chain);response.responseStr += "---AFilter()";}}public class BFilter implements Filter {@Overridepublic void doFilter(Request request, Response response, FilterChain chain) {request.requestStr = request.requestStr.replace("天朝", "中国");chain.doFilter(request, response, chain);response.responseStr += "---BFilter()";}}public class FilterChain implements Filter {List<Filter> filters = new ArrayList<Filter>();int index = 0;public FilterChain addFilter(Filter f) {this.filters.add(f);return this;}@Overridepublic void doFilter(Request request, Response response, FilterChain chain) {if(index == filters.size()) return ;Filter f = filters.get(index);index ++;f.doFilter(request, response, chain);}}public class Main {public static void main(String[] args) {String msg = "*********************";Request request = new Request();request.setRequestStr(msg);Response response = new Response();response.setResponseStr("response");FilterChain fc = new FilterChain();fc.addFilter(new AFilter()) .addFilter(new BFilter()) ;fc.doFilter(request, response, fc);System.out.println(request.getRequestStr());System.out.println(response.getResponseStr());}}
0 0
- 责任链模式(Chain of Responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链(chain of responsibility)模式
- 责任链模式(Chain of Responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链模式(chain of responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链模式(Chain of Responsibility)
- 行为型模式-----责任链模式(chain of responsibility)
- 浅谈设计模式:责任链模式(Chain Of Responsibility)
- Java设计模式----责任链模式(Chain of Responsibility)
- 设计模式:责任链模式(Chain of Responsibility)
- 设计模式之---责任链模式(Chain of Responsibility)
- 设计模式:责任链模式(Chain of Responsibility)
- Java责任链模式(Chain of Responsibility模式)
- Android 消息机制
- 单链表的实现 来自算法精解-c语言描述
- Mantle 初步使用
- iOS发布流程
- UINavigationController,UIViewController与UITabBarController的整合使用
- 责任链模式(Chain of Responsibility)
- uva 572
- HSV(HSV颜色模型)
- iframe自适应高度问题
- 判断是否为AJAX 请求原理
- PL/SQL学习笔记
- 使用Mantle处理Model层对象
- static全局、局部变量,全局变量及局部变量
- mysql varchar 的最大长度