Chain of Responsibility - PowerPoint PPT Presentation

About This Presentation
Title:

Chain of Responsibility

Description:

... reflects the statement that 'tools are always assigned to tool carts' ... The ToolCart code reflects the statement that 'tool carts have a responsible engineer' ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 22
Provided by: Asatisfied206
Category:

less

Transcript and Presenter's Notes

Title: Chain of Responsibility


1
Chain of Responsibility
  • Ian Price
  • Dale Willey

2
We 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.

4
Glorified If-Else if Statement?
5
Answer!
6
Where can this be Used?
7
Examples
  • GUI help program
  • TCL object naming
  • TK imaging
  • Try-Throw-Catch
  • New Age Networking
  • Different Algorithms
  • Visualization Schemes

8
Challenge 12.1
How does the Chain of Responsibility pattern
differ from ordinary method lookup?
9
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

10
Challenge 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)
13
Answer 12.2
14
Challenge 12.3
Write getResponsible() for A.
MachineComponent B. Tool C. ToolCart
15
Answer 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
16
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
17
Challenge 12.4
Fill in the constructors in the next to support a
design that ensures that ever Machine Component
object has a responsible engineer.
18
Answer 12.4
19
Challenge 12.5
Cite an example when the Chain of Responsibility
pattern might occur if the chained objects do not
form a composite.
20
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 users text.

21
Questions? Comments?Rants? Raves? (Actually,
wed prefer you keep the rants to yourself)
Write a Comment
User Comments (0)
About PowerShow.com