1. 程式人生 > >十四、ChainOfResponsibility 責任鏈模式

十四、ChainOfResponsibility 責任鏈模式

oid fin trac spec rgs ide boolean rri limits

設計:

技術分享圖片

代碼清單:

Trouble:

public class Trouble {
    private int number;
    public Trouble(int number){
        this.number = number;
    }
    public int getNumber(){
        return number;
    }
    public String toString(){
        return "[Trouble"+number+"]";
    }
}

Support:

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 solved by "+this+"."); } protected void fail(Trouble trouble){ System.out.println(trouble+"cannot be resolved."); } }

SpecialSupport

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;
        }
    }
}

OddSupport

public class OddSupport extends  Support{
    public OddSupport(String name){
        super(name);
    }

    @Override
    protected boolean resolve(Trouble trouble) {
        if(trouble.getNumber()%2==1){
            return true;
        }else{
            return false;
        }
    }
}

NoSupport

public class NoSupport extends Support{
    public NoSupport(String name){
        super(name);
    }

    @Override
    protected boolean resolve(Trouble trouble) {
        return false;
    }
}

LimitSupport

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;
        }
    }
}

Main:

public class Main {
    public static void main(String[] args){
        Support alice = new NoSupport("Alice");
        Support bob = new LimitSupport("BoB",100);
        Support charlie = new SpecialSupport("charlie",429);
        Support diana = new LimitSupport("Diana",200);
        Support elmo = new OddSupport("Elmo");
        Support fred = new LimitSupport("Fred",300);

        alice.setNext(bob).setNext(charlie).setNext(diana).setNext(elmo).setNext(fred);

        for(int i=0;i<500;i+=33){
            alice.support(new Trouble(i));
        }
    }
}

十四、ChainOfResponsibility 責任鏈模式