Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage.

Similar presentations


Presentation on theme: "Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage."— Presentation transcript:

1 Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage system. How can we efficiently handle requests made by users or client objects? We don’t want to be forced to explicitly hardwire handler relationships or request and handler connections. The Chain of Responsibility design pattern gives us an efficient solution to this problem where there are a variable number of possible requests from a user or a client object. This pattern allows us to pass on requests to a receiving object that maybe able to handle to the request, if a particular object cannot handle a request, then that request is continually passed on until an object is able to handle the it.

2 Chain of Responsibility Objects in the chain pass the request along until an object is able to handle it. The number and type of objects(handlers) are not known by the user or client object. The CoR pattern avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. The receiving objects are chained together and requests are passed down along the chain. The first object in the chain receives the request and will either handle it or forward it to the next object on the chain and so on. The important thing to remember, is that the object that made the request, has no knowledge of which object is handling the request. The request has an implicit receiver.

3 Chain of Responsibility Each object in the chain can share a common interface for handling requests and accessing its successor in the chain. As well instead of sending and receiving objects keeping references to all other receiving objects, each sending object keeps a reference to the head of the chain, and each receiving object keeps a reference to its immediate successor. Use the CoR pattern when more than one object can handle a request and when the handling object isn’t known ahead of time. When you want to issue a request to one of several objects, without specifying the object explicitly. When you need to dynamically specify the group of objects that can handle requests.

4 Example All small children cause damage, but how should they be punished? Based upon the dollar amount of damage done by the child, the family must decide who is to punish their child. Who in this Chain of Responsibility is to punish the child; The Grandma, the Mother, the Father, or something worse...muwhaahahahaaa.

5 UML Diagram of Chain of responsibility

6 Code abstract class KidsPunished { public int least = 15; public KidsPunished successor; public void setSuccessor(KidsPunished successor) { this.successor = successor; } abstract public void processRequest(PunishRequest request); }

7 More Code class Grandma extends KidsPunished { public int tolerance = 10 * least; public void processRequest(PunishRequest request) { if( request.getAmount() < tolerance ) System.out.println("Amount is $" + request.getAmount()+ ", so Grandma jobs to punish"); else if( successor != null) successor.processRequest(request); }

8 Code class Mother extends KidsPunished { private final double tolerance = 20 * least; public void processRequest(PunishRequest request) { if(request.getAmount() < tolerance) System.out.println("Amount is $" + request.getAmount()+ ", so Mom's jobs to punish"); else if( successor != null) successor.processRequest(request); }

9 Code class Father extends KidsPunished { public int tolerance = 30 * least; public void processRequest(PunishRequest request) { if(request.getAmount() < tolerance) System.out.println("Amount is $" + request.getAmount()+ ", so Father's jobs to punish"); else if(successor != null) successor.processRequest(request); }

10 class HigherPower extends KidsPunished { public int tolerance = 100 * least; public void processRequest(PunishRequest request) { if(request.getAmount() < tolerance) System.out.println("Amount is $" + request.getAmount()+ ", pray for forgiveness"); else if(successor != null) successor.processRequest(request); }

11 Class PunishRequest { public double amount; public PunishRequest( double amount) { this.amount = amount; } public double getAmount() { return amount; } public void setAmount(double amt){ amount = amt; }

12 Finally its Punishing Time!! class PunishingTime { public static void main(String[] args) { Grandma granny = new Grandma(); Mother momma = new Mother(); Father dad = new Father(); HigherPower god = new HigherPower(); granny.setSuccessor(momma); momma.setSuccessor(dad); dad.setSuccessor(god); try { while (true) { System.out.println("Enter the amount in Damages your child caused."); System.out.print(" : "); double dollar= Double.parseDouble(new BufferedReader(new InputStreamReader(System.in)).readLine()); granny.processRequest(new PunishRequest( dollar)); } }catch(Exception e) { System.exit(1); }


Download ppt "Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage."

Similar presentations


Ads by Google