Top Banner
25

What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Page 2: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

What is the Chain?

• It’s a behavioral design pattern.

• It deals with how objects make requests and how they are handled.

Page 3: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Question

• When an object makes a request, who handles it?

Page 4: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Conventional Approach

• The object that makes the request sends it directly to the object what handles it.

Page 5: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Drawbacks

• Objects must know who handles each type of request.

• Objects must have links to many other objects.

Page 6: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Chain Approach

• Objects are arranged in a “chain of responsibility.”

• Requests are passed along the chain until they are handled.

Page 7: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Advantages

• Objects don’t care who handles their request.

• Objects only need one link, to their successor in the chain.

• Multiple objects have a chance to handle requests.

Page 8: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Example:Context Sensitive Help

Page 9: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Suppose:

• User can click any part of the interface to get specific help.

• If no specific help is available, more general information is provided.

Page 10: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Use the Chain Pattern!

• When the user wants help, the object he clicks on sends a request down the chain.

• If an object in the chain can provide the needed help, it handles the request.

• Otherwise, it passes the request to its successor.

Page 11: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Flow of Requests

Page 12: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Implementation

• Each object must have a HandleHelp() method.

• This method either handles the help request or passes it along the chain.

Page 13: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Interaction Diagram

Page 14: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Implementation (cont.)

• We define a HelpHandler class with a HandleHelp() method.

• All other classes are subclasses to HelpHandler.

Page 15: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Implementation (cont.)

• By default, the HandleHelp() method passes the request to the next object in the chain.

• If an object is to handle a request, HandleHelp() must be overloaded.

Page 16: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Class Diagram

Page 17: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Things to Consider

Page 18: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

When to Use the Chain

• If a request may be handled my multiple objects.

• If the handler isn’t know ahead of time.

• If you don’t want to explicitly specify the handler.

Page 19: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Linking Objects

• Existing links between objects may be used if they fit your desired chain.

OR

• Designated links can be included in the handler class.

Page 20: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Request Types

• Request types can be hard coded in the handler class.

OR

• A parameter can be passed to indicate the type of request and how it should be handled.

Page 21: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Pros and Cons

Page 22: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Pros

• Coupling between objects is reduced.

• Sender and receiver of request need no explicit knowledge of one another.

• Chain allows flexibility in assigning responsibilities to objects.

Page 23: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Cons

• Care must be taken to ensure all requests are handled properly.

Page 24: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

References

• Gamma, Erich et al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.

Page 25: What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.

Questions?