Title: Chain of Responsibility
1Chain of Responsibility
2We Will Cover
- What is Chain of Responsibility
- A few examples
- The Metsker Challenges
3- 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.
4Glorified If-Else if Statement?
5Answer!
6Where can this be Used?
7Examples
- GUI help program
- TCL object naming
- TK imaging
- Try-Throw-Catch
- New Age Networking
- Different Algorithms
- Visualization Schemes
8Challenge 12.1
How does the Chain of Responsibility pattern
differ from ordinary method lookup?
9Answer 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
10Challenge 12.2
Redraw the diagram in Figure 12.1, moving
getResponsible() to SimulatedItem and adding this
behavior to Tool. Code and picture follow
11 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
12(No Transcript)
13Answer 12.2
14Challenge 12.3
Write getResponsible() for A.
MachineComponent B. Tool C. ToolCart
15Answer 12.3
A 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
16Answer 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
17Challenge 12.4
Fill in the constructors in the next to support a
design that ensures that ever Machine Component
object has a responsible engineer.
18Answer 12.4
19Challenge 12.5
Cite an example when the Chain of Responsibility
pattern might occur if the chained objects do not
form a composite.
20Answer 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 users text.
21Questions? Comments?Rants? Raves? (Actually,
wed prefer you keep the rants to yourself)