1 / 25

What is the Chain?

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?. Conventional Approach. The object that makes the request sends it directly to the object what handles it.

gilbertop
Download Presentation

What is the Chain?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. Drawbacks • Objects must know who handles each type of request. • Objects must have links to many other objects.

  5. Chain Approach • Objects are arranged in a “chain of responsibility.” • Requests are passed along the chain until they are handled.

  6. 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.

  7. Example:Context Sensitive Help

  8. Suppose: • User can click any part of the interface to get specific help. • If no specific help is available, more general information is provided.

  9. 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.

  10. Flow of Requests

  11. Implementation • Each object must have a HandleHelp() method. • This method either handles the help request or passes it along the chain.

  12. Interaction Diagram

  13. Implementation (cont.) • We define a HelpHandler class with a HandleHelp() method. • All other classes are subclasses to HelpHandler.

  14. 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.

  15. Class Diagram

  16. Things to Consider

  17. 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.

  18. 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.

  19. 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.

  20. Pros and Cons

  21. 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.

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

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

  24. Questions?

More Related