Chain of Responsibility Pattern Gof pp. 223-232Yuyang Chen.

Slides:



Advertisements
Similar presentations
Design Patterns David Talby. This Lecture Representing other objects Proxy, Adapter, Façade Re-routing method calls Chain of Responsibility Coding partial.
Advertisements

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.
Decorator & Chain of Responsibility Patterns Game Design Experience Professor Jim Whitehead February 2, 2009 Creative Commons Attribution 3.0 (Except for.
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.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns David Talby. This Lecture n The rest of the pack u Working over a network F Proxy, State, Chain of Responsibility u Working with external.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns David Talby. This Lecture n Re-Routing Method Calls u Proxy, Chain of Responsibility n Working with external libraries u Adapter, Façade.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Pattern Oriented Design Chain of Responsibility From:Design Patterns Gamma. Johnson Helm. Vlissides (GoF) Present: F 楊汝康 R 徐德皓 R 高稚翔.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Strategy Design Patterns CS 590L - Sushil Puradkar.
Lexi case study (Part 2) Presentation by Matt Deckard.
GoF Sections Design Problems and Design Patterns.
Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Architectural pattern: Interceptor Source: POSA II pp 109 – 140POSA II Environment: developing frameworks that can be extended transparently Recurring.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage.
Design Patterns Introduction
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
1/23/2018 Design Patterns David Talby.
Abstract Factory Pattern
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Observer Design Pattern
Software Design and Architecture
Leftover Patterns Chain of Responsibility
Abstract Factory Pattern
Object Oriented Analysis and Design
Design Patterns Satya Puvvada Satya Puvvada.
Informatics 122 Software Design II
Behavioral Patterns Part-I introduction UNIT-VI
Behavioral Design Pattern
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
More About Inheritance & Interfaces
Strategy Design Pattern
Informatics 122 Software Design II
Decorator Pattern.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Chain of Responsibility Pattern Gof pp Yuyang Chen

Chain of Responsibility Pattern

Intent Intent Avoid coupling sender of request to its receiver by giving more than one object chance to handle request. Chain receiving objects and pass request along until an object handles it. Avoid coupling sender of request to its receiver by giving more than one object chance to handle request. Chain receiving objects and pass request along until an object handles it.

Chain of Responsibility Pattern Motivation: Motivation: Book Example – Consider context-sensitive help for graphical user interface Book Example – Consider context-sensitive help for graphical user interface Let’s say user clicks on a help button – help provided depends on the button clicks Let’s say user clicks on a help button – help provided depends on the button clicks Help request is handled by one of several user interface objects, but which one depends on context and specificity of available help. Help request is handled by one of several user interface objects, but which one depends on context and specificity of available help. Problem is that object that provides help isn’t known by the object that request help. Problem is that object that provides help isn’t known by the object that request help.

Chain of Responsibility Pattern Description Description Decouple senders and receivers by giving multiple objects (in a set order) a chance to handle a request. Request passed until an object handles it. Decouple senders and receivers by giving multiple objects (in a set order) a chance to handle a request. Request passed until an object handles it. 1 st object in chain receives request – either handles it or forwards to next object in chain. 1 st object in chain receives request – either handles it or forwards to next object in chain. Object that makes request has no explicit knowledge of who will handle it – request has an implicit receiver. Object that makes request has no explicit knowledge of who will handle it – request has an implicit receiver.

Chain of Responsibility Pattern Request! Request Handled!

Chain of Responsibility Pattern Structure Structure

Chain of Responsibility Pattern Participants: Handler – defines interface for handling requests. Can also implement successor link Handler – defines interface for handling requests. Can also implement successor link ConcreteHandler – handles requests it is responsible for; otherwise forwards requests to successor. ConcreteHandler – handles requests it is responsible for; otherwise forwards requests to successor. Client – initiates request to a ConcreteHandler in the chain. Client – initiates request to a ConcreteHandler in the chain.

Chain of Responsibility Pattern Use Chain of Responsibility when: Use Chain of Responsibility when: More than 1 object may handle a request, and handle isn’t known beforehand. More than 1 object may handle a request, and handle isn’t known beforehand. Want to issue request to one of several objects without specifying receiver explicitly. Want to issue request to one of several objects without specifying receiver explicitly. Set of objects that can handle a request should be specificed dynamically Set of objects that can handle a request should be specificed dynamically

Chain of Responsibility Pattern CONSEQUENCES Benefits: Benefits: Decoupling of senders and receivers Decoupling of senders and receivers Added flexibility Added flexibility Sender doesn’t need to know specifically who the handlers are Sender doesn’t need to know specifically who the handlers are Potential Drawbacks: Potential Drawbacks: Client can’t explicity specify who handles a request Client can’t explicity specify who handles a request No guarantee of request being handled (request falls off end of chain) No guarantee of request being handled (request falls off end of chain)

Chain of Responsibility Pattern Implementation How to implement the successor chain? How to implement the successor chain? Define new links (in Handler or ConcreteHandler) Define new links (in Handler or ConcreteHandler) Use existing links Use existing links (such as the parent references from the Composite pattern) (such as the parent references from the Composite pattern)

Chain of Responsibility Pattern Sample Code: Sample Code: class HelpHandler { public: HelpHandler(HelpHandler* s) : _successor(s) { } virtual void HandleHelp(); private: HelpHandler* _successor; }; void HelpHandler::HandleHelp() { if(_successor) { _succeessor->HandleHelp(); }

Chain of Responsibility Pattern Representing Requests Representing Requests Request can be hard-coded operation. This is convenient and safe, but can only forward fixed set of requests that Handler class defines. Request can be hard-coded operation. This is convenient and safe, but can only forward fixed set of requests that Handler class defines. Can use separate request objects that contain all request parameters; these must have accessor functions that return an identifier for the class Can use separate request objects that contain all request parameters; these must have accessor functions that return an identifier for the class

Chain of Responsibility Pattern void Handler::HandleRequest (Request * theRequest) { switch (theRequest->GetKind() ) { case Help: // cast argument ot appropriate type HandleHelp( (HelpRequest*) theRequest); break; case Print: HandlePrint((PrintRequest*) theRequest); // etc... } Subclasses can extend this by overriding HandleRequest to handle only the requests in which the subclass is interested.