首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

责任链形式(Chain of Responsibility Method)

2014-04-25 
责任链模式(Chain of Responsibility Method)package responsibility/** *create by datao.wang 2014-2-1

责任链模式(Chain of Responsibility Method)
package responsibility;/** *create by datao.wang 2014-2-11-下午5:03:32 *抽象处理者 */public abstract class Handler { //处理方法 可以添加处理的请求对象作为参数public abstract void handler(HandlerChain chain);}package responsibility;import java.util.ArrayList;import java.util.List;/** *create by datao.wang 2014-2-11-下午6:52:34 *请求处理者管理链 */public class HandlerChain extends Handler {//下一个请求处理者protected List<Handler> handler=new ArrayList<Handler>();int index=0;@Overridepublic void handler(HandlerChain chain) {if(index==handler.size())return;//循环完了退出if(handler.size()!=0){//循环处理请求Handler h=handler.get(index++);h.handler(chain);}}public HandlerChain addHandler(Handler h){handler.add(h);return this;}}package responsibility;/** *create by datao.wang 2014-2-11-下午5:08:24 *具体请求处理者1 */public class Handler1 extends Handler {@Overridepublic void handler(HandlerChain chain) {if("xxx".equals("如果请求该handler1处理,否则把责任往下传递")){System.out.println("handler1 处理......");}else {System.out.println("handler1-----抛出");chain.handler(chain);}}}package responsibility;/** *create by datao.wang 2014-2-11-下午5:08:24 *具体请求处理者2 */public class Handler2 extends Handler {@Overridepublic void handler(HandlerChain chain) {if("xxx".equals("如果请求该handler2处理,否则把责任往下传递")){System.out.println("handler2 处理......");}else {System.out.println("handler2-----抛出");chain.handler(chain);}}}package responsibility;/** *create by datao.wang 2014-2-11-下午5:08:24 *具体请求处理者3 */public class Handler3 extends Handler {@Overridepublic void handler(HandlerChain chain) {if("xxx".equals("如果请求该handler3处理,否则把责任往下传递")){System.out.println("handler3 处理......");}else {System.out.println("handler3-----抛出");chain.handler(chain);}}}

?

热点排行