Chain of Responsibility

Similar documents
Chain of Responsibility

Sometimes it's necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.

Proliferating interconnections tend to reduce object reuse. It can be difficult to change the system's behavior in any significant way.

Composition class is responsible for maintaining and updating the linebreaks of text displayed in a text viewer.

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Visitor. Intent. Motivation

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Design Patterns. Collectional Patterns. Session objectives 11/06/2012. Introduction. Composite pattern. Iterator pattern

One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it.

Chapter 1 Introduction. What is a Design Pattern? Design Patterns in Smalltalk MVC

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

CS5530 Mobile/Wireless Systems Using Google Map in Android

16/07/2012. Design Patterns. By Võ Văn Hải Faculty of Information Technologies - HUI. Behavioral Patterns. Session objectives. Strategy.

1 Getting and Extracting the Upgrader

» Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request!



1 Getting and Extracting the Upgrader

VMware AirWatch SDK Plugin for Apache Cordova Instructions Add AirWatch Functionality to Enterprise Applicataions with SDK Plugins

Quick Guide on implementing SQL Manage for SAP Business One

Configuring Database & SQL Query Monitoring With Sentry-go Quick & Plus! monitors

History of Java. VM (Java Virtual Machine) What is JVM. What it does. 1. Brief history of Java 2. Java Version History

The Chain of Responsibility Pattern

The Chain of Responsibility Pattern. Design Patterns In Java Bob Tarr

API Gateway Version September Visual Mapper User Guide

COP2800 Homework #3 Assignment Spring 2013

User Guide. Document Version: 1.0. Solution Version:

TechNotes. Revision 5/23/2011. TS7 Features

Faculty Textbook Adoption Instructions

CS510 Concurrent Systems Class 2. A Lock-Free Multiprocessor OS Kernel

TRAINING GUIDE. Lucity Mobile

Synoptic Display Studio Developers Guide

CSE 361S Intro to Systems Software Lab #2

These tasks can now be performed by a special program called FTP clients.

Marian Online 2 Instructor Manual 12

UML : MODELS, VIEWS, AND DIAGRAMS

Please contact technical support if you have questions about the directory that your organization uses for user management.

Common Language Runtime

But for better understanding the threads, we are explaining it in the 5 states.

Element Creator for Enterprise Architect

INSERTING MEDIA AND OBJECTS

Using CppSim to Generate Neural Network Modules in Simulink using the simulink_neural_net_gen command

Whitepaper. Migrating External Specs to AutoCAD Plant 3D. Set Up the Required Folder Structure. Migrating External Specs to AutoCAD Plant 3D

DECISION CONTROL CONSTRUCTS IN JAVA

Class CustomActivity... 2 Field Detail Constructor Detail Method Detail... 13

TIBCO Statistica Options Configuration

Maximo Reporting: Maximo-Cognos Metadata

ROCK-POND REPORTING 2.1

IMPORTING INFOSPHERE DATA ARCHITECT MODELS INFORMATION SERVER V8.7

Lab 4. Name: Checked: Objectives:

Table of Contents. 1 Introduction Connecting to the API HTTP request syntax API release versions... 4

Getting the Most from REST and JSON

Hands-On Lab. Lab Manual HOL057 Data Features in Windows Forms 2.0

Essentials for IBM Cognos BI (V10.2) Day(s): 5. Overview

Demand Forecasting. For. Microsoft Dynamics 365 for Operations. Technical Guide. Release 7.1. December 2017

SGL Observatory Automation. ASCOM Motor Focuser Control Getting Started Guide

Introduction to Programming ArcObjects using the Microsoft.Net Framework

Objectives. Topic 8: Input, Interaction, & Introduction to callbacks. Input Devices. Project Sketchpad. Introduce the basic input devices

Programming in C/C++ Lecture 3

Knowledgeware Rule-based Clash

Asset Panda Web Application Release 12.02

Using the Swiftpage Connect List Manager

Customer Information. Agilent 2100 Bioanalyzer System Startup Service G2949CA - Checklist

TRAINING GUIDE. Overview of Lucity Spatial

Custodial Integrator. Release Notes. Version 3.11 (TLM)

1 Getting and Extracting the Upgrader

Municode Website Instructions

Launching Xacta 360 Marketplace AMI Guide June 2017

Test Pilot User Guide

The following screens show some of the extra features provided by the Extended Order Entry screen:

CUSTOM CONTROLS. Pieter Saelens Henri Reterink. Building for the future. Better, faster, everywhere.

Admin Report Kit for Exchange Server

RISKMAN REFERENCE GUIDE TO USER MANAGEMENT (Non-Network Logins)

MyUni Adding Content. Date: 29 May 2014 TRIM Reference: D2013/ Version: 1

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

Using the Swiftpage Connect List Manager

Network Rail ARMS - Asbestos Risk Management System. Training Guide for use of the Import Survey Template

App Center User Experience Guidelines for Apps for Me

TRACK CHAIRS CREATING INVITED SESSIONS AND INVITING SESSION ORGANIZER(S)

Task 1 High-Level Object-Oriented Class Specification Create Initial Design Classes Designing Boundary Classes

On the road again. The network layer. Data and control planes. Router forwarding tables. The network layer data plane. CS242 Computer Networks

Second Assignment Tutorial lecture

Outlook Web Application (OWA) Basic Training


Because this underlying hardware is dedicated to processing graphics commands, OpenGL drawing is typically very fast.

Transferring dongle licenses online

SmartPass User Guide Page 1 of 50

Release Date: 29-April-2011 Purpose: The Configuration & Orchestration Manager Release Notes provide the following information:

Assignment #5: Rootkit. ECE 650 Fall 2018

How to Share Your Google Calendar

Internet Explorer Configuration Reference

CS5530 Mobile/Wireless Systems Swift

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

USPS Picture Permit indicia

Introduction to Eclipse

Course 10262A: Developing Windows Applications with Microsoft Visual Studio 2010 OVERVIEW

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2

Scroll down to New and another menu will appear. Select Folder and a new

User Guide. ACE Data Source. OnCommand Workflow Automation (WFA) Abstract PROFESSIONAL SERVICES

Transcription:

Chain f Respnsibility 1 Intent Avid cupling the sender f a request t its receiver by giving mre than ne bject a chance t handle the request. Chain the receiving bjects and pass the request alng the chain until an bject handles it. Mtivatin A cntext-sensitive help facility fr a graphical user interface. The prblem here is that the bject that ultimately prvides the help isn't knwn explicitly t the bject (e.g., the buttn) that initiates the help request. The bject that made the request has n explicit knwledge f wh will handle it we say the request has an implicit receiver. T frward the request alng the chain, and t ensure receivers remain implicit, each bject n the chain shares a cmmn interface fr handling requests and fr accessing its successr n the chain.

2 Applicability Use Chain f Respnsibility when mre than ne bject may handle a request, and the handler isn't knwn a priri. The handler shuld be ascertained autmatically. yu want t issue a request t ne f several bjects withut specifying the receiver explicitly. the set f bjects that can handle a request shuld be specified dynamically. Structure

Participants 3 Handler (HelpHandler) defines an interface fr handling requests. (ptinal) implements the successr link. CncreteHandler (PrintButtn, PrintDialg) handles requests it is respnsible fr. can access its successr. Client Cllabratins if the CncreteHandler can handle the request, it des s; therwise it frwards the request t its successr. initiates the request t a CncreteHandler bject n the chain. When a client issues a request, the request prpagates alng the chain until a CncreteHandler bject takes respnsibility fr handling it. Cnsequences 2. Reduced cupling. It can simplify bject intercnnectins. Instead f bjects maintaining references t all candidate receivers, they keep a single reference t their successr. 3. Added flexibility in assigning respnsibilities t bjects. Yu can add r change respnsibilities fr handling a request by adding t r therwise changing the chain at run-time. Yu can cmbine this with subclassing t specialize handlers statically. 4. Receipt isn't guaranteed. Implementatin 1. Implementing the successr chain. a. Define new links (usually in the Handler, but CncreteHandlers culd define them instead). b. Use existing links. Fr example, parent references in a part-whle hierarchy can define a part's successr. 2. Cnnecting successrs (fr new links). The Handler nt nly defines the interface fr the requests but usually maintains the successr as well.

class HelpHandler { HelpHandler(HelpHandler* s) : _successr(s) { virtual vid HandleHelp(); private: HelpHandler* _successr; ; 4 vid HelpHandler::HandleHelp () { if (_successr) { _successr->handlehelp(); 1. Representing requests. a. The request is a hard-cded peratin invcatin. Yu can frward nly the fixed set f requests that the Handler class defines. a. Use a single handler functin that takes a request cde (e.g., an integer cnstant r a string) as parameter. This supprts an pen-ended set f requests. The sender and receiver agree n hw the request shuld be encded. It requires cnditinal statements fr dispatching the request based n its cde. There's n type-safe way t pass parameters, s they must be packed and unpacked manually. Use separate request bjects that bundle request parameters. A Request class can represent requests explicitly, and new kinds f requests can be defined by subclassing. Handlers must knw the kind f request (that is, which Request subclass they're using) t access these parameters. vid Handler::HandleRequest (Request* therequest) { switch (therequest->getkind()) { case Help: // cast argument t apprpriate type HandleHelp((HelpRequest*) therequest); case Print: HandlePrint((PrintRequest*) therequest); default:

Subclasses can extend the dispatch by verriding HandleRequest. The subclass handles nly the requests in which it's interested; ther requests are frwarded t the parent class. In this way, subclasses effectively extend (rather than verride) the HandleRequest peratin. class ExtendedHandler : public Handler { virtual vid HandleRequest(Request* therequest); ; vid ExtendedHandler::HandleRequest (Request* therequest) { switch (therequest->getkind()) { case Preview: // handle the Preview request 5 default: // let Handler handle ther requests Handler::HandleRequest(theRequest); Sample Cde typedef int Tpic; cnst Tpic NO_HELP_TOPIC = -1; class HelpHandler { HelpHandler(HelpHandler* = 0, Tpic = NO_HELP_TOPIC); virtual bl HasHelp(); virtual vid SetHandler(HelpHandler*, Tpic); virtual vid HandleHelp(); private: HelpHandler* _successr; Tpic _tpic; ; HelpHandler::HelpHandler ( HelpHandler* h, Tpic t ) : _successr(h), _tpic(t) { bl HelpHandler::HasHelp () { return _tpic!= NO_HELP_TOPIC; vid HelpHandler::HandleHelp () { if (_successr!= 0) { _successr->handlehelp(); class Widget : public HelpHandler { prtected: Widget(Widget* parent, Tpic t = NO_HELP_TOPIC); private: Widget* _parent; ; Widget::Widget (Widget* w, Tpic t) : HelpHandler(w, t) { _parent = w;

6 class Buttn : public Widget { Buttn(Widget* d, Tpic t = NO_HELP_TOPIC); virtual vid HandleHelp(); // Widget peratins that Buttn verrides... ; Buttn::Buttn (Widget* h, Tpic t) : Widget(h, t) { vid Buttn::HandleHelp () { if (HasHelp()) { // ffer help n the buttn else { HelpHandler::HandleHelp(); class Dialg : public Widget { Dialg(HelpHandler* h, Tpic t = NO_HELP_TOPIC); virtual vid HandleHelp(); ; // Widget peratins that Dialg verrides... Dialg::Dialg (HelpHandler* h, Tpic t) : Widget(0) { SetHandler(h, t); vid Dialg::HandleHelp () { if (HasHelp()) { // ffer help n the dialg else { HelpHandler::HandleHelp(); class Applicatin : public HelpHandler { Applicatin(Tpic t) : HelpHandler(0, t) { ; virtual vid HandleHelp(); // applicatin-specific peratins... vid Applicatin::HandleHelp () { // shw a list f help tpics cnst Tpic PRINT_TOPIC = 1; cnst Tpic PAPER_ORIENTATION_TOPIC = 2; cnst Tpic APPLICATION_TOPIC = 3; Applicatin* applicatin = new Applicatin(APPLICATION_TOPIC); Dialg* dialg = new Dialg(applicatin, PRINT_TOPIC); Buttn* buttn = new Buttn(dialg, PAPER_ORIENTATION_TOPIC); buttn->handlehelp();

7 Knwn Uses Several class libraries use the Chain f Respnsibility pattern t handle user events. MacApp and ET++ : EventHandler TCL library : Bureaucrat NeXT's AppKit : Respnder. In the Unidraw framewrk fr graphical editrs, a cmpnent r a cmpnent view may frward cmmand interpretatin t its parent, which may in turn frward it t its parent, and s n, thereby frming a chain f respnsibility. ET++ uses Chain f Respnsibility t handle graphical update. A graphical bject calls the InvalidateRect peratin whenever it must update a part f its appearance. The default implementatin f InvalidateRect frwards the request t the enclsing cntainer bject. The last bject in the frwarding chain is a Windw instance. Related Patterns Chain f Respnsibility is ften applied in cnjunctin with Cmpsite.