Welcome to my Chain of Responsibility Design Pattern Tutorial! Wow, that was a mouthful!
This pattern has a group of objects that are expected to between them be able to solve a problem. If the first Object can’t solve it, it passes the data to the next Object in the chain. In this tutorial, I’ll use it to make the right calculations based off of a String request. While that is pretty simple the capabilities of this pattern are endless.
The code follows the video to help you learn.
If you think videos like this are nice, it helps to tell Google with a button click [googleplusone]
Share if you think others may like this
Code from the Video
CHAIN.JAVA
// The chain of responsibility pattern has a // group of objects that are expected to between // them be able to solve a problem. // If the first Object can't solve it, it passes // the data to the next Object in the chain public interface Chain { // Defines the next Object to receive the data // if this Object can't process it public void setNextChain(Chain nextChain); // Either solves the problem or passes the data // to the next Object in the chain public void calculate(Numbers request); }
NUMBERS.JAVA
// This object will contain 2 numbers and a // calculation to perform in the form of a String public class Numbers { private int number1; private int number2; private String calculationWanted; public Numbers(int newNumber1, int newNumber2, String calcWanted){ number1 = newNumber1; number2 = newNumber2; calculationWanted = calcWanted; } public int getNumber1(){ return number1; } public int getNumber2(){ return number2; } public String getCalcWanted(){ return calculationWanted; } }
ADDNUMBERS.JAVA
public class AddNumbers implements Chain{ private Chain nextInChain; // Defines the next Object to receive the // data if this one can't use it public void setNextChain(Chain nextChain) { nextInChain = nextChain; } // Tries to calculate the data, or passes it // to the Object defined in method setNextChain() public void calculate(Numbers request) { if(request.getCalcWanted() == "add"){ System.out.print(request.getNumber1() + " + " + request.getNumber2() + " = "+ (request.getNumber1()+request.getNumber2())); } else { nextInChain.calculate(request); } } }
SUBTRACTNUMBERS.JAVA
public class SubtractNumbers implements Chain{ private Chain nextInChain; @Override public void setNextChain(Chain nextChain) { nextInChain = nextChain; } @Override public void calculate(Numbers request) { if(request.getCalcWanted() == "sub"){ System.out.print(request.getNumber1() + " - " + request.getNumber2() + " = "+ (request.getNumber1()-request.getNumber2())); } else { nextInChain.calculate(request); } } }
MULTNUMBERS.JAVA
public class MultNumbers implements Chain{ private Chain nextInChain; @Override public void setNextChain(Chain nextChain) { nextInChain = nextChain; } @Override public void calculate(Numbers request) { if(request.getCalcWanted() == "mult"){ System.out.print(request.getNumber1() + " * " + request.getNumber2() + " = "+ (request.getNumber1()*request.getNumber2())); } else { nextInChain.calculate(request); } } }
DIVIDENUMBERS.JAVA
public class DivideNumbers implements Chain{ private Chain nextInChain; @Override public void setNextChain(Chain nextChain) { nextInChain = nextChain; } @Override public void calculate(Numbers request) { if(request.getCalcWanted() == "div"){ System.out.print(request.getNumber1() + " / " + request.getNumber2() + " = "+ (request.getNumber1()/request.getNumber2())); } else { System.out.print("Only works for add, sub, mult, and div"); } } }
TESTCALCCHAIN.JAVA
public class TestCalcChain { public static void main(String[] args){ // Here I define all of the objects in the chain Chain chainCalc1 = new AddNumbers(); Chain chainCalc2 = new SubtractNumbers(); Chain chainCalc3 = new MultNumbers(); Chain chainCalc4 = new DivideNumbers(); // Here I tell each object where to forward the // data if it can't process the request chainCalc1.setNextChain(chainCalc2); chainCalc2.setNextChain(chainCalc3); chainCalc3.setNextChain(chainCalc4); // Define the data in the Numbers Object // and send it to the first Object in the chain Numbers request = new Numbers(4,2,"add"); chainCalc1.calculate(request); } }
i didnt back again. i was always here. cos i have learned a lot from you. your recent topic is java so i am little bit irregular in this blog. i like your php, wp, css tutorials. Thanks.
That’s nice to hear. Are you able to access YouTube yet? Do you know of any other video sites that you can access? Maybe I can put them there. I only use YouTube because I never got much attention on the other sites. youTube has gone out of its way to help me
would you help me to know how to use email template ?
I’m not sure what you mean? Give me a bit more information and I’ll try to help
There is a coding error.
In class Numbers method is misspelled.
So in some classes there is an error.
Could you correct getcalcWanted to getCalcWanted, please?
And thank you for material. 🙂
I fixed that. Thank you for pointing out the error. I have no idea how I did that?
Note to add:
getCalcWanted is spelled differently in various class so you have errors. 😉
and btw Great Material and presentation. 😀
Even my english not very well I find that your presentation and material are very good and perfect and easy to understund. We are waiting and waiting for other courses to explore and to understand Java and design pattern.
Thank you very much 🙂 I have many other videos in the works
You are great! I love your tutorials! But mabe I’m stupid, but I wonder how you know how meny classes you need for this pattern to work out? Is it always this many?
Thank you 🙂 The design patterns are rather flexible and they are by no means always exactly like I describe here. If you ever need to try and solve a problem by passing it off to a list of objects, then this is one way to accomplish that. I hope that helps