Follow treslines by email clicking Here!

Wednesday, August 20, 2014

Programming Design Pattern - Chain of Responsibility Pattern Applied - Best Practise


Hi there!

Today i'm gonna show the Chain of Responsibility design pattern in action. The Chain of Responsibility design pattern is a very useful programming design pattern while dealing processes that happens in sequences.

I'm assuming here you already know the concepts and i'll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. You can always come back here, take it, adapt it and use it in your applictions as you may need. So be sure you bookmark it or join the group here on the right side of this post subscribing it.

First of all, let's take a look at the UML diagram of it. After that we will take the analogy for our example.

The UML Diagram of the Chain of Responsibility Pattern


Pay close attention, because once you understand that, everything will become clear and simple to understand. That's the reason I'm putting always the UML first. That way you'll get an eye for it with the time.




The example

In our example we will see, how we could implement a customer after sales feedback robot. How could we do it? In this example we will see it.

The abstract chain

This is the base class to be implemented by all participants of this chain.

public abstract class Inspector < T > {
    protected T toInspect;
    protected Inspector < T > next;
    public Inspector(Inspector < T > next) {this.next = next;}
    public void inspect(T toInspect){
        this.toInspect=toInspect;
        if(toInspect==responsibility()){
          System.out.println(responsibility() + " " + action());
        }toNextInspector();
    }
    public abstract T responsibility();
    public abstract T action();
    public void toNextInspector(){
        if(next instanceof NullObject){return;}
        next.inspect(toInspect);
    }
    public static class NullObject extends Inspector < String > {
        public NullObject() {super(null);}
        public String responsibility() {return null;}
        public String action() {return null;}
    }
}

The participants of the chain

Every participant has a single responsibility and gets a chance to process it. If it can not handle it, it will pass it over to the next till the end.

public class Complaint extends Inspector < String > {
    public Complaint(Inspector < String > next) {super(next);}
    public String responsibility() {return "Reclamação";}
    public String action() {return "Enviar pro departamento de qualidade!";}
}
public class Billing extends Inspector < String > {
    public Billing(Inspector < String > next) {super(next);}
    public String responsibility() {return "Cobrança";}
    public String action() {return "Enviar pro departamento de pos-vendas!";}
}
public class Praise extends Inspector < String > {
    public Praise(Inspector < String > next) {super(next);}
    public String responsibility() {return "Elogio";}
    public String action() {return "Enviar pro RH!";}
}
public class Suggestion extends Inspector < String > {
    public Suggestion(Inspector < String > next) {super(next);}
    public String responsibility() {return "Sugestão";}
    public String action() {return "Enviar para o setor de desenvolvimento!";}
}
public class Spam extends Inspector < String > {
    public Spam(Inspector < String > next) {super(next);}
    public String responsibility() {return "Spam";}
    public String action() {return "Jogar no lixo!";}
}

The Test

Finally, let´s see how our customer after sales robot works. :)

public class Client {
    public static void main(String[] args) {
        Inspector < String > inspector;
        String[] incomingPost = new String[] { 
                "ABC", 
                "Spam", 
                "Elogio", 
                "Reclamação", 
                "Sugestão",
                "Cobrança" };
        for (String post : incomingPost) {
            inspector = new Spam(new Complaint(new Praise(new Billing(new Suggestion(new Inspector.NullObject())))));
            inspector.inspect(post);
        }
    }
}

That's all. Hope you like it!

No comments:

Post a Comment