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

Slides:



Advertisements
Similar presentations
Chain of Responsibility Pattern Gof pp Yuyang Chen.
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
The Singleton Pattern II Recursive Linked Structures.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Inheritance Inheritance Reserved word protected Reserved word super
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Twin A Design Pattern for Modeling Multiple Inheritance Mahmoud ghorbanzadeh.
Zaki Alasadi Supervisor:Dr noorhosseini.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Scenario: Audio Clip Imagine that your application played an audio clip Based on user action, a different audio clip may begin playing You want only.
March 2007ACS Ron McFadyen1 Façade simplifies access to a related set of objects by providing one object that all objects outside the set use to.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
November Ron McFadyen1 Façade simplifies access to a related set of objects by providing one object that all objects outside the set use to.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Pattern Oriented Design Chain of Responsibility From:Design Patterns Gamma. Johnson Helm. Vlissides (GoF) Present: F 楊汝康 R 徐德皓 R 高稚翔.
Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Team 6 “The Unparseables” Design Patterns Chain of Responsibility Observer Flyweight 1.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Interfaces and Inner Classes
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces and Polymorphism CS 162 (Summer 2009).
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Observer Pattern Context:
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Classes and Objects.
Design Patterns C++ Java C#.
Interface.
Design Patterns C++ Java C#.
Leftover Patterns Chain of Responsibility
C# Event Processing Model
null, true, and false are also reserved.
Delegates & Events 1.
Behavioral Design Pattern
class PrintOnetoTen { public static void main(String args[]) {
slides created by Ethan Apter
Chapter 9 Carrano Chapter 10 Small Java
slides created by Ethan Apter
Presentation transcript:

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.

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.

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.

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.

UML Diagram of Chain of responsibility

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); }

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); }

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); }

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); }

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); }

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

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); }