责任链模式:将多个对象组成一条责任链,然后按照它们在责任链上的顺序一个一个地找出到低应该谁来负责处理。
使用责任链模式可以弱化“请求方”和“处理方”之间的关联关系,让双方各自都称为独立复用的组件。
角色:
Handler处理者:定了处理请求的接口API,Handler角色知道下一个处理者是谁。如果自己无法处理请求,它会将请求转给“下一个处理者”,本实例中有support类扮演。
ConcreteHandler具体的处理者:负责处理请求的具体角色,本实例中由NOSupport、LimitSupport等扮演。
Client请求者:向第一个ConcreteHandler角色发送请求的角色,本示例中由Main方法扮演。
代码:
public class Trouble { private int number; public Trouble(int number) { this.number = number; } public int getNumber() { return this.number; } public String toString() { return "[Trouble "+number+" ]"; } }
public abstract class Support { private String name; private Support next; public Support(String name) { this.name = name; } public Support setNext(Support next) { this.next = next; return next; } public final void support(Trouble trouble) { if(resolve(trouble)) { done(trouble); }else if(next != null){ next.support(trouble); }else { fail(trouble); } } public String toString() { return "["+name+"]"; } protected abstract boolean resolve(Trouble trouble); protected void done(Trouble trouble) { System.out.println(trouble + " is resolve by "+this); } protected void fail(Trouble trouble) { System.out.println(trouble + " cannot by resolve by "+this); } }
public class NoSupport extends Support { public NoSupport(String name) { super(name); } @Override protected boolean resolve(Trouble trouble) { return false; } }
public class LimitSupport extends Support { private int limit; public LimitSupport(String name,int limit) { super(name); this.limit = limit; } @Override protected boolean resolve(Trouble trouble) { if(trouble.getNumber()<limit) { return true; }else { return false; } } }
public class SpecialSupport extends Support { private int number; public SpecialSupport(String name, int number) { super(name); this.number = number; } @Override protected boolean resolve(Trouble trouble) { if(trouble.getNumber() == number) { return true; }else { return false; } } }
public class OldSupport extends Support { public OldSupport(String name) { super(name); } @Override protected boolean resolve(Trouble trouble) { if(trouble.getNumber()%2 ==1) { return true; }else { return false; } } }
public class Main { public static void main(String[] args) { Support noSupport = new NoSupport("noSupport"); Support limitSupport = new LimitSupport("limitSupport",10); Support oldSupport = new OldSupport("oldSupport"); Support specialSupport = new SpecialSupport("specialSupport",11); noSupport.setNext(limitSupport).setNext(oldSupport).setNext(specialSupport); for(int i =0;i<100;i++) { noSupport.support(new Trouble(i)); } } }
执行结果:
[Trouble 0 ] is resolve by [limitSupport]
[Trouble 1 ] is resolve by [limitSupport]
[Trouble 2 ] is resolve by [limitSupport]
[Trouble 3 ] is resolve by [limitSupport]
[Trouble 4 ] is resolve by [limitSupport]
[Trouble 5 ] is resolve by [limitSupport]
[Trouble 6 ] is resolve by [limitSupport]
[Trouble 7 ] is resolve by [limitSupport]
[Trouble 8 ] is resolve by [limitSupport]
[Trouble 9 ] is resolve by [limitSupport]
[Trouble 10 ] cannot by resolve by [specialSupport]
[Trouble 11 ] is resolve by [oldSupport]
[Trouble 12 ] cannot by resolve by [specialSupport]
[Trouble 13 ] is resolve by [oldSupport]
[Trouble 14 ] cannot by resolve by [specialSupport]
[Trouble 15 ] is resolve by [oldSupport]
[Trouble 16 ] cannot by resolve by [specialSupport]