+ All Categories
Home > Documents > Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few...

Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few...

Date post: 18-Dec-2015
Category:
View: 213 times
Download: 0 times
Share this document with a friend
21
Chain of Responsibility Ian Price Dale Willey
Transcript
Page 1: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Chain of Responsibility

Ian Price

Dale Willey

Page 2: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

We Will Cover

What is Chain of Responsibility

A few examples

The Metsker Challenges

Page 3: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

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

Page 4: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Glorified If-Else if Statement?

Page 5: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Answer!

Page 6: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Where can this be Used?

Page 7: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Examples

GUI help program

TCL object naming

TK imaging

Try-Throw-Catch

New Age Networking

Different Algorithms

Visualization Schemes

Page 8: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Challenge 12.1

How does the Chain of Responsibility pattern differ from ordinary method lookup?

Page 9: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Answer 12.1

Method lookup searches across a well-defined series of classes. The Chain of Responsibility pattern directs the search for a responsible method to occur across a series of objects

The mechanics of method lookup are part of the Java language specification, whereas Chain of Responsibility is unders your control as a developer

Page 10: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Challenge 12.2

Redraw the diagram in Figure 12.1, moving getResponsible() to SimulatedItem and adding this behavior to Tool. Code and picture follow

Page 11: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

{ public Engineer getResponsible(SimulatedItem item)

{

if (item instanceof Tool)

{

Tool t = (Tool) item;

return t.getToolCart().getResponsible();

}

if (item instanceof MachineComponent)

{ MachineComponent c = (MachineComponent) item;

if (c.getResponsible() == null)

{

if (c.getParent() != null)

{ return c.getParent().getResponsible(); }

}

}

return null;

}

}

Page 12: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.
Page 13: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Answer 12.2

Page 14: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Challenge 12.3

Write getResponsible() for

A. MachineComponent

B. Tool

C. ToolCart

Page 15: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Answer 12.3A MachineComponent object may have an explicitly assigned responsible person. If it doesn't, it passes the request to its parent:

public Engineer getResponsible()

{

if (responsible != null)

{

return responsible;

}

if (parent != null)

{

return parent.getResponsible();

}

return null;

}

Page 16: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Answer 12.3 (cont)The code for Tool.getResponsibility() reflects the statement that "tools are always assigned to tool carts":

public Engineer getResponsible()

{

return toolCart.getResponsible();

}

The ToolCart code reflects the statement that "tool carts have a responsible engineer":

public Engineer getResponsible()

{

return responsible;

}

Page 17: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Challenge 12.4Fill in the constructors in the next to support a design that ensures that ever Machine Component object has a responsible engineer.

Page 18: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Answer 12.4

Page 19: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Challenge 12.5

Cite an example when the Chain of Responsibility pattern might occur if the chained objects do not form a composite.

Page 20: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Answer 12.5

A chain of on-call engineers that follows a standard rotation. If the primary on-call engineer does not answer a production support page in a specific amount of time, the notification system pages the next engineer in the chain.

When users enter such information as the date of an event, a chain of parsers can take turns trying to decode the user’s text.

Page 21: Chain of Responsibility Ian Price Dale Willey. We Will Cover What is Chain of Responsibility A few examples The Metsker Challenges.

Questions? Comments?Rants? Raves?

(Actually, we’d prefer you keep the rants to yourself)


Recommended