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

Post on 18-Dec-2015

215 views 0 download

Transcript of 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 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.

Glorified If-Else if Statement?

Answer!

Where can this be Used?

Examples

GUI help program

TCL object naming

TK imaging

Try-Throw-Catch

New Age Networking

Different Algorithms

Visualization Schemes

Challenge 12.1

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

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

Challenge 12.2

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

{ 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;

}

}

Answer 12.2

Challenge 12.3

Write getResponsible() for

A. MachineComponent

B. Tool

C. ToolCart

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;

}

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;

}

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

Answer 12.4

Challenge 12.5

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

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.

Questions? Comments?Rants? Raves?

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