Responsibility chain model-chain of Responsibility, example 1
In this pattern, each recipient typically contains a reference to another recipient. If an object cannot handle the request, it passes the same request to the next recipient, and so on.
Trouble class
This class is: the problem to be handled by the chain of responsibility trouble class.
This example is simpler and trouble has only one int as the number to be processed.
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 Abstract class
The support class is a unified definition of the node abstraction in the chain of responsibility.
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; } Protected abstract Boolean solve (trouble trouble); protected void done (trouble trouble) { //Resolve System.out.println (trouble + "is resolved by" + This + "."); } protected void fail (Trouble trouble) { //unresolved System.out.println (trouble + "cannot be resolved."); Public void execute (Trouble trouble) { if (Solve (trouble)) {done (trouble); } else if (next! = NULL) { next.execute (trouble); } else { fail (trouble); } } Public String toString () { return ' [' + name + '] '; }}
Zerosupport class
This class is specifically designed to handle the trouble number 0
/** * This class is specifically designed to handle situations where trouble is 0 */public class Zerosupport extends support {public zerosupport (String name) { super (n AME); } @Override protected Boolean solve (Trouble trouble) { if (trouble.getnumber () = = 0) { return true; } else { return false;}} }
Limitsupport class
As long as the trouble number is less than the value of the member variable limit of this class, then an instance of the Limitsupport class can handle this trouble
/** * This class is specifically designed to handle cases where the trouble value is less than limit */public class Limitsupport extends support { private int limit; Public Limitsupport (String name, int limit) { super (name); This.limit = limit; } @Override protected Boolean solve (Trouble trouble) { if (Trouble.getnumber () < limit) { return true;< c20/>} else { return false;}} }
Oddsupport class
As long as the trouble number is odd, then an instance of the Oddsupport class can handle the trouble.
/** * This class is designed to handle trouble in odd cases */public class Oddsupport extends support {public oddsupport (String name) { super (NA me); } @Override protected Boolean solve (Trouble trouble) { if (trouble.getnumber ()% 2 = = 1) { return true; } el SE { return false;}} }
Main
For Test runs
public class Main {public static void Main (string[] args) {support charlie = new Zerosupport ("Charlie"); Support Bob = new Limitsupport ("Bob", +); Support Diana = new Limitsupport ("Diana", +); Support Elmo = new Oddsupport ("Elmo"); Support Fred = new Limitsupport ("Fred", +); form responsibility chain Charlie.setnext (Bob). Setnext (Diana). Setnext (Elmo). Setnext (Fred); Create various problems and let this chain of responsibility handle for (int i = 0; i <; i + =) { Charlie.execute (new Trouble (i));} }}
Responsibility chain mode-chain of Responsibility (Java implementation), example 1