Chain of Responsibility Ian Price Dale Willey We

  • Slides: 21
Download presentation
Chain of Responsibility Ian Price Dale Willey

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

Avoid coupling the sender of a request to its receiver by giving more than

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?

Glorified If-Else if Statement?

Answer!

Answer!

Where can this be Used?

Where can this be Used?

Examples GUI help program TCL object naming TK imaging Try-Throw-Catch New Age Networking Different

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

Challenge 12. 1 How does the Chain of Responsibility pattern differ from ordinary method lookup?

Answer 12. 1 Method lookup searches across a welldefined series of classes. The Chain

Answer 12. 1 Method lookup searches across a welldefined 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 get. Responsible() to

Challenge 12. 2 Redraw the diagram in Figure 12. 1, moving get. Responsible() to Simulated. Item and adding this behavior to Tool. Code and picture follow

{ public Engineer get. Responsible(Simulated. Item item) { if (item instanceof Tool) { Tool

{ public Engineer get. Responsible(Simulated. Item item) { if (item instanceof Tool) { Tool t = (Tool) item; return t. get. Tool. Cart(). get. Responsible(); } if (item instanceof Machine. Component) { Machine. Component c = (Machine. Component) item; if (c. get. Responsible() == null) { if (c. get. Parent() != null) { return c. get. Parent(). get. Responsible(); } } } return null; } }

Answer 12. 2

Answer 12. 2

Challenge 12. 3 Write get. Responsible() for A. Machine. Component B. Tool C. Tool.

Challenge 12. 3 Write get. Responsible() for A. Machine. Component B. Tool C. Tool. Cart

Answer 12. 3 A Machine. Component object may have an explicitly assigned responsible person.

Answer 12. 3 A Machine. Component object may have an explicitly assigned responsible person. If it doesn't, it passes the request to its parent: public Engineer get. Responsible() { if (responsible != null) { return responsible; } if (parent != null) { return parent. get. Responsible(); } return null; }

Answer 12. 3 (cont) The code for Tool. get. Responsibility() reflects the statement that

Answer 12. 3 (cont) The code for Tool. get. Responsibility() reflects the statement that "tools are always assigned to tool carts": public Engineer get. Responsible() { return tool. Cart. get. Responsible(); } The Tool. Cart code reflects the statement that "tool carts have a responsible engineer": public Engineer get. Responsible() { return responsible; }

Challenge 12. 4 Fill in the constructors in the next to support a design

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.

Answer 12. 4

Answer 12. 4

Challenge 12. 5 Cite an example when the Chain of Responsibility pattern might occur

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

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? Raves? (Actually, we’d prefer you keep the rants to yourself)

Questions? Comments? Raves? (Actually, we’d prefer you keep the rants to yourself)