Chain of Responsibility



Similar documents
Chapter 3 Chapter 3 Service-Oriented Computing and SOA Lecture Note

SAP Training System Access

The WebShop E-Commerce Framework

MANAGEMENT S ROLE 1/16/ Copyright 2001, Net Objectives

Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

Organization. Introduction to Software Engineering

Applying Design Patterns in Distributing a Genetic Algorithm Application

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Tutorial: Getting Started

Excerpts from Chapter 4, Architectural Modeling -- UML for Mere Mortals by Eric J. Naiburg and Robert A. Maksimchuk

Xtreme RUP. Ne t BJECTIVES. Lightening Up the Rational Unified Process. 2/9/2001 Copyright 2001 Net Objectives 1. Agenda

NetBeans and GlassFish v 2.1 Creating a Healthcare Facility Visual Web Application

Information systems modelling UML and service description languages


The Basic Java Applet and JApplet

How to develop your own app

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

How To Test In Bluej

Gadget: A Tool for Extracting the Dynamic Structure of Java Programs

Hands-On Lab. Client Workflow. Lab version: Last updated: 2/23/2011

Microsoft PowerPoint Exercises 4

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

Principles of Software Construction: Objects, Design, and Concurrency. Course Introduction. toad. toad Fall School of Computer Science

Creating Database Tables in Microsoft SQL Server

TLMC WORKSHOP: THESIS FORMATTING IN WORD 2010

I219 Software Design Methodology

Rapid Application Development of a Decision Support System using Object Oriented Programming

Mesa DMS. Once you access the Mesa Document Management link, you will see the following Mesa DMS - Microsoft Internet Explorer" window:

All Tech Notes and KBCD documents and software are provided "as is" without warranty of any kind. See the Terms of Use for more information.

New World Construction FTP service User Guide

CREATING AND EDITING CONTENT AND BLOG POSTS WITH THE DRUPAL CKEDITOR

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Microsoft Lync 2013 TRAINING GUIDE. University Of Central Florida Computer Services & Telecommunications

How To Develop A Web Dialog For An Org Database With A Database On A Computer (Oracle)

BPM Scheduling with Job Scheduler

Graphic Objects and Loading Them into TGF2/MMF2

Installing and configuring Microsoft Reporting Services

IBM Tivoli Composite Application Manager for WebSphere

How To Insert Hyperlinks In Powerpoint Powerpoint

Structural Design Patterns Used in Data Structures Implementation

Assignment # 2: Design Patterns and GUIs

Tabs3, PracticeMaster, and the pinwheel symbol ( trademarks of Software Technology, Inc. Portions copyright Microsoft Corporation

The WebShop e-commerce framework

Introduction. There are several bits of information that must be moved:

TIBCO Hawk SNMP Adapter Installation

Design Report: Resource Management Software CS400 Senior Design I

SystemTools Software Inc. NTFS and Share Permissions Reporting

Secrets of Event Viewer for Active Directory Security Auditing Lepide Software

Making a Web Page with Microsoft Publisher 2003

Design Patterns. Design patterns are known solutions for common problems. Design patterns give us a system of names and ideas for common problems.

How to install and set up the WinSCP software for Secure File Transfer

Using TFrame to create more advanced TJSEditDialogs in LMD DialogPack

Designing and Writing a Program DESIGNING, CODING, AND DOCUMENTING. The Design-Code-Debug Cycle. Divide and Conquer! Documentation is Code

Boot Camp Installation & Setup Guide

Editors Comparison (NetBeans IDE, Eclipse, IntelliJ IDEA)

Level 13: Creating Yes / No Player Options

How To Create A C++ Web Service

A Comparison of Programming Languages for Graphical User Interface Programming

Add in Guide for Microsoft Dynamics NAV May 2012

SharePoint 2007 Get started User Guide. Team Sites

Access Tutorial 1 Creating a Database

Singletons. The Singleton Design Pattern using Rhapsody in,c

How to install and use the File Sharing Outlook Plugin

How Do I Connect My Mac with Bluetooth Devices?

Using scanners with Axapta Document Management

Welcome to the Brookdale Community College Online Employment System Applicant Tutorial

Flash Tutorial Part I

Tutorial Build a simple IBM Rational Publishing Engine (RPE) template for IBM Rational DOORS

- Training. Getting Started with Outlook Information Technology Services - Training The University of Texas at Austin

IBM TCP/IP Network Port Monitor

Database Operations (Backup/Restore/Move/Manage) Technical Support Engineering Rosslare Security NA For more information please see

Establish an Import Letter of Credit

Outlook 2010 Desk Reference Guide

Optimal Binary Search Trees Meet Object Oriented Programming

CS3600 SYSTEMS AND NETWORKS

Programming Language Constructs as Basis for Software Architectures

Using Windows Task Scheduler instead of the Backup Express Scheduler

Netbeans IDE Tutorial for using the Weka API

What's a backup cycle?

BusinessObjects Enterprise XI Release 2

Architectural Patterns (3)

IP Phone Services Configuration

Windows XP Pro: Basics 1

To begin, visit this URL:

Version of this tutorial: 1.06a (this tutorial will going to evolve with versions of NWNX4)

Twin A Design Pattern for Modeling Multiple Inheritance

Title: SharePoint Advanced Training

Digitally Sign an InfoPath form and Submit using

Outlook Profile Setup Guide Exchange 2010 Quick Start and Detailed Instructions

Remote Monitoring and Control of the R&S FSV with a Web Browser

dobe Acrobat XI Pro Digital Signatures

Transcription:

Chain of Responsibility Comp-303 : Programming Techniques Lecture 21 Alexandre Denault Computer Science McGill University Winter 2004 April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 1

Last lecture... Flyweights allows us to deal with situation where a large number of objects could be required. There is still a lot of work to do on the project and not a lot of time to do it. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 2

Typical Situation Today, our example starts with a help system. When a user presses F1 on his keyboard, he brings up a help screen for the object currently in focus. Consider the screen on the following screen on the next slides : Given that not every object has a help screen, which help screen should be shown? April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 3

Typical Save As Dialog Box Save As... _ X Directory_1 Directory_2 Directory_3 File_1 File_2 filename filetype OK Cancel April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 4

Flyweight Pattern Name : Chain of Responsibility Classification : Behavioral Intent : Avoid coupling the sender of a request to its receiver by giving more that one object the chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 5

Motivation When an object doesn t have a help screen associated to it, we would like to see the help request forwarded to a lower level. For example, if the OK button doesn t have a help screen, then the request should be forwarded to the dialog box. If the dialog box doesn t have a help screen, then the request should be forwarded to the main screen. If the main screen doesn t have a help screen, then the request should be forwarded to the application. And so on... April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 6

Motivation (cont.) The main idea is that when F1 is pressed, the application doesn t know what item will answer the request. An item in the application knows one of two things: How it should answer a request for help. OR Who should it pass along the request for help. The items don t need to know anything about requests it can t handle. It only needs to know how to pass them along. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 7

Motivation (cont.) Keyboard Event (F1) OK Button Dialog Box Main Screen Application Help() Help() Help() Help() April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 8

Applicability Use Chain of Responsibility when...... more than one object may handle a request, and the handler isn t known ahead of time.... you want to issue a request to one of several objects without specifying the receiver explicitly.... the set of objects that can handle a request should be specified dynamically. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 9

Structure Client Handler successor: Handler HandleRequest() ConcreteHandler1 successor: Handler HandleRequest() ConcreteHandler2 successor: Handler HandleRequest() April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 10

Participants Handler : defines an interface for handling requests and sometimes implements the successor link ConcreteHandler : handles the request or forwards it to the successor Client : initiates the request to one of the ConcreteHandler in the chain April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 11

Collaborations The client issues the request to the ConcreteHandler. The request is then propagated along the chain until a ConcreteHandler takes responsibility. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 12

Consequences Reduce coupling : The pattern frees the receiving object of knowing who should handle the request. Objects in the chain don t need to know the structure of the chain. Added flexibility: You can add or change responsibilities for handling a request by changing the chain at run time. Reception is not guaranteed: Nothing in the pattern guarantees that the request will be handled. A request can fall of the chain if the chain is not properly configured. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 13

Implementation The are two ways to implement the successor chain: Use existing links : some structures ( such has trees ) already have the necessary links to implement this pattern. Create new links : if the handlers have no links (or the existing ones are unsuitable), you will need to implement your own links. Which brings us to the next point: the chain doesn t have to be linear, it can have the shape of a tree. Finally, you can make the handler more flexible by representing the request as on object and passing it along as a parameter. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 14

Does this look familiar? public abstract class Server { public void receive(message msg) { switch(msg->gettype()) { case PlayerMove: //do something case GraphicUpdate: //do something case ChatMessage: //do something case GameMessage: //do something default: //We ve got trouble April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 15

Sample Code public abstract class Handler { private Handler successor = null; public abstract boolean receive(message msg); // Returns true if message is handled. public void setsuccessor(handler su) { this.successor = su; public boolean nextsuccessor(message msg){ if (this.successor == null) return false; return this.successor.receive(msg); April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 16

Building our handlers public class GraphicHandler extends Handler { public boolean receive(message msg) { if (msg is related to graphics) { // handle request return true; else { return nextsuccessor(msg); April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 17

Using our handlers public class Server { Handler graphicmsghandler; Handler playermsghandler; Handler chatmsghandler; Handler gamemsghandler; public Server() { graphicmsghandler = new GraphicHandler(); playermsghandler = new PlayerHandler(); chatmsghandler = new ChatHandler(); gamemsghandler = new GameHandler(); graphicmsghandler.setsuccessor(playermsghandler); playermsghandler.setsuccessor(chatmsghandler); chatmsghandler.setsuccessor(gamemsghandler); April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 18

Using our handlers public void receive(message msg) { boolean msghandle = graphicmsghandler(msg); if (msghandle == false { //We ve got trouble April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 19

Notes This has a lot more overhead than using switch statements. However, this is dynamic and modular. The server doesn t need to know anything about message types. Message types that are received most often should be at the front of the chain. As mentioned previously, the chain can be dynamic. April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 20

Dynamic Chain public disablechat() { playermsghandler.setsuccessor(gamemsghandler); public enablechat() { playermsghandler.setsuccessor(chatmsghandler); April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 21

Known Uses Event Handlers in Guis April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 22

Related Patterns Composite April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 23

Summary Chain of Responsibility is a useful pattern when you want to decouple sender and receiver. It allows you to use objects to handle events dynamically (i.e. dynamic switch statements). April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 24

Tool of the day: JProfiler A profiler is a computer program that can track the performance of another program, thereby finding bottle necks. If your having performance problems with your project, a profiler can help you pinpoint the problem spots. JProfiler is an example of such a tool. It is very easy to use. Unfortunately it s commercial. There are many other Java profiler out there ( JMP, Extensible Java Profiler (EJP), YourKit Java Profiler, etc). IBM s and Sun s JVM even have a built-in Java profiler (but it s kinda unreliable and hard to use). java -Xrunhprof:cpu=times classname java -Xrunhprof:heap=sites classname April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 25

References These slides are inspired (i.e. copied) from these three books. Design Patterns, Elements of Reusable Object-Oriented Software; Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides; Addison Wesley; 1995 Java Design Patterns, a Tutorial; James W. Cooper Addison Wesly; 2000 Design Patterns Explained, A new Perspective on Object Oriented Design; Alan Shalloway, James R. Trott; Addison Wesley; 2002 April 1, 2004 Lecture 21 Comp 303 : Chain of Responsibility Page 26