chain of responsibilityadenau/teaching/cs303/lecture21.pdf · april 1, 2004 lecture 21 { comp 303 :...

26
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

Upload: others

Post on 21-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Chain of Responsibility

Comp-303 : Programming Techniques

Lecture 21

Alexandre DenaultComputer ScienceMcGill University

Winter 2004

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 1

Page 2: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Last lecture . . .

• Flyweights allows us to deal with situation where a largenumber of objects could be required.

• There is still a lot of work to do on the project and not a lot oftime to do it.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 2

Page 3: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Typical Situation

• Today, our example starts with a help system.

• When a user presses F1 on his keyboard, he brings up a helpscreen 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 screenshould be shown?

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 3

Page 4: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

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

Page 5: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Flyweight

• Pattern Name : Chain of Responsibility

• Classification : Behavioral

• Intent : Avoid coupling the sender of a request to its receiverby giving more that one object the chance to handle therequest. Chain the receiving objects and pass the request alongthe chain until an object handles it.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 5

Page 6: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Motivation

• When an object doesn’t have a help screen associated to it, wewould like to see the help request forwarded to a lower level.

• For example, if the OK button doesn’t have a help screen, thenthe request should be forwarded to the dialog box.

• If the dialog box doesn’t have a help screen, then the requestshould be forwarded to the main screen.

• If the main screen doesn’t have a help screen, then the requestshould be forwarded to the application.

• And so on . . .

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 6

Page 7: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Motivation (cont.)

• The main idea is that when F1 is pressed, the applicationdoesn’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’thandle. It only needs to know how to pass them along.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 7

Page 8: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Motivation (cont.)

KeyboardEvent (F1)

OKButton

DialogBox

MainScreen

Application

Help()

Help()

Help()

Help()

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 8

Page 9: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Applicability

Use Chain of Responsibility when . . .

• . . . more than one object may handle a request, and the handlerisn’t known ahead of time.

• . . . you want to issue a request to one of several objects withoutspecifying the receiver explicitly.

• . . . the set of objects that can handle a request should bespecified dynamically.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 9

Page 10: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Structure

Handler

successor: Handler

HandleRequest()

ConcreteHandler2

successor: Handler

HandleRequest()

ConcreteHandler1

successor: Handler

HandleRequest()

Client

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 10

Page 11: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Participants

• Handler : defines an interface for handling requests andsometimes implements the successor link

• ConcreteHandler : handles the request or forwards it to thesuccessor

• Client : initiates the request to one of the ConcreteHandler inthe chain

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 11

Page 12: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Collaborations

• The client issues the request to the ConcreteHandler.

• The request is then propagated along the chain until aConcreteHandler takes responsibility.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 12

Page 13: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Consequences

• Reduce coupling : The pattern frees the receiving object ofknowing who should handle the request. Objects in the chaindon’t need to know the structure of the chain.

• Added flexibility: You can add or change responsibilities forhandling a request by changing the chain at run time.

• Reception is not guaranteed: Nothing in the pattern guaranteesthat the request will be handled. A request can fall of thechain if the chain is not properly configured.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 13

Page 14: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

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 theexisting ones are unsuitable), you will need to implementyour own links.

• Which brings us to the next point: the chain doesn’t have tobe linear, it can have the shape of a tree.

• Finally, you can make the handler more flexible by representingthe request as on object and passing it along as a parameter.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 14

Page 15: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

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

Page 16: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

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

Page 17: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

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

Page 18: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

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

Page 19: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

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

Page 20: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Notes

• This has a lot more overhead than using switch statements.

• However, this is dynamic and modular. The server doesn’tneed to know anything about message types.

• Message types that are received most often should be at thefront of the chain.

• As mentioned previously, the chain can be dynamic.

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 20

Page 21: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Dynamic Chain

public disableChat() {

playerMsgHandler.setSuccessor(gameMsgHandler);

}

public enableChat() {

playerMsgHandler.setSuccessor(chatMsgHandler);

}

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 21

Page 22: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Known Uses

• Event Handlers in Guis

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 22

Page 23: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Related Patterns

• Composite

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 23

Page 24: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Summary

• Chain of Responsibility is a useful pattern when you want todecouple 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

Page 25: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

Tool of the day: JProfiler

• A profiler is a computer program that can track theperformance of another program, thereby finding bottle necks.

• If your having performance problems with your project, aprofiler 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, ExtensibleJava Profiler (EJP), YourKit Java Profiler, etc).

• IBM’s and Sun’s JVM even have a built-in Java profiler (butit’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

Page 26: Chain of Responsibilityadenau/teaching/cs303/lecture21.pdf · April 1, 2004 Lecture 21 { Comp 303 : Chain of Responsibility Page 13. Implementation † The are two ways to implement

References

• These slides are inspired (i.e. copied) from these three books.

– Design Patterns, Elements of Reusable Object-OrientedSoftware; Erich Gamma, Richard Helm, Ralph Johnson andJohn Vlissides; Addison Wesley; 1995

– Java Design Patterns, a Tutorial; James W. CooperAddison Wesly; 2000

– Design Patterns Explained, A new Perspective on ObjectOriented Design; Alan Shalloway, James R. Trott; AddisonWesley; 2002

April 1, 2004 Lecture 21 – Comp 303 : Chain of Responsibility Page 26