vendredi 22 septembre 2017

Chain of Responsibility Design pattern implementation with starting stopping the chain in different points

i have simple implementation of Chain of Responsibility Design pattern which
traversing the chain from the start to end of the chain , but now i want to make
the chain start from a certain point and end in s certain reten point.

For Example here is simple pattern implementation in JAVA here i want to the chain stop in B , what is the best way to implement it ?

public static void main(String[] args) {
        PlanetHandler chain = setUpChain();
        //Stop in B dont continue to C
        chain.handleRequest(TypeEnum.B);

}


public enum TypeEnum {
    A, B, C;
}

public abstract class Handler {
    Handler successor;
    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }
    public abstract void handleRequest(TypeEnum t);
}


public class A extends Handler {

    public void handleRequest(TypeEnum t) {     
            if (successor != null) {
                successor.handleRequest(request);
            }
        }
    }
}

public class B extends Handler {

    public void handleRequest(TypeEnum t) {     
            if (successor != null) {
                successor.handleRequest(request);
            }
        }
    }
}

public class C extends Handler {

    public void handleRequest(TypeEnum t) {     
            if (successor != null) {
                successor.handleRequest(request);
            }
        }
    }
}

public static Handler setUpChain() {
        Handler a = new A();
        Handler b = new B();
        Handler c = new C();
        a.setSuccessor(b);
        b.setSuccessor(c);
        return a;
}

Aucun commentaire:

Enregistrer un commentaire