设计模式 - chain of Responsibility
Chain of Responsibility也就是职责链模式,通过使用链式结构,使对象都有机会处理请求,从而避免请求的发送者与接受者间的耦合关系。将这些对象连成链,并沿着该链传递请求,直到有对象处理为止。
实现的关键在于处理对象的转发机制,也就是如果我能处理,那么我处理,如果不能处理,就转发给后面的对象处理。但普通的实现方法中,该处理对象必须保有后面对象的引用,也就是仍存在一定程度的耦合。理想的情况是处理对象(handler)之间彼此不知对方,通过另外的机制完成对象传递。
Apache有开源框架实现职责链模式,用户可以通过配置xml来决定请求的处理顺序,而handler只需实现固定接口即可。
举个例子:
某皮包公司有各种总,但这些总共享三个秘书(不要理解错了),分为为lilyzhou, LucyWang和RuHua。总们签署的文件会交给秘书们处理,lilyzhou是大秘,LucyWang是二秘,RuHua是三秘。liliy不愿意处理的文件交给LucyWang,LucyWang不愿意处理的交给Ruhua。ruhua没有不愿意的权利,只能自己处理。
秘书们的特殊爱好如下:lilyzhou只处理boss.zhang签署的文件(她认为自己是秘书中的老大),LucyWang只处理boss.li签署的文件,Ruahua处理她们不愿意的处理的文件(有人的地方就有江湖啊)
下面贴出代码:
package com.inspur.jiyq.designpattern.responsibilitychain; public interface Secretary { public void handover(ImporantFile file); public void dealWithIt(ImporantFile file); }
package com.inspur.jiyq.designpattern.responsibilitychain; public class ImporantFile { /** * 文件签名 boss.zhang, boss.li, bossWang */ private String signature; public String getSignature() { return signature; } public void setSignature(String signature) { this.signature = signature; } }
package com.inspur.jiyq.designpattern.responsibilitychain; import com.inspur.jiyq.designpattern.responsibilitychain.impl.LilyZhou; public class BossOnDuty { public static void main(String[] args) { ImporantFile file = new ImporantFile(); file.setSignature("boss.sun"); Secretary secretary = new LilyZhou(); secretary.handover(file); } }
package com.inspur.jiyq.designpattern.responsibilitychain.impl; import com.inspur.jiyq.designpattern.responsibilitychain.ImporantFile; import com.inspur.jiyq.designpattern.responsibilitychain.Secretary; public class LilyZhou implements Secretary { private Secretary lucyWang = new LucyWang(); @Override public void handover(ImporantFile file) { if (file.getSignature().equals("boss.zhang")) { dealWithIt(file); } else { lucyWang.handover(file); } } @Override public void dealWithIt(ImporantFile file) { // TODO Auto-generated method stub System.out.println("lilyzhou deal with it."); } }
package com.inspur.jiyq.designpattern.responsibilitychain.impl; import com.inspur.jiyq.designpattern.responsibilitychain.ImporantFile; import com.inspur.jiyq.designpattern.responsibilitychain.Secretary; public class LucyWang implements Secretary{ private Secretary ruhua = new RuHua(); @Override public void handover(ImporantFile file) { if (file.getSignature().equals("boss.li")) { dealWithIt(file); } else { ruhua.handover(file); } } @Override public void dealWithIt(ImporantFile file) { System.out.println("lucyWang deal with it."); } }
package com.inspur.jiyq.designpattern.responsibilitychain.impl; import com.inspur.jiyq.designpattern.responsibilitychain.ImporantFile; import com.inspur.jiyq.designpattern.responsibilitychain.Secretary; public class RuHua implements Secretary { @Override public void handover(ImporantFile file) { // RuHua can not deliever it to anybody else dealWithIt(file); } @Override public void dealWithIt(ImporantFile file) { System.out.println("RuHua deal with it."); } }