Chapter 8. Behavioral Patterns: Chain of Responsibility and Command

The two behavioral patterns we will explore in this chapter—Chain of Responsibility and Command—are concerned with passing requests for action to appropriate objects. The respective patterns choose the objects to pass the requests to in different ways.

Chain of Responsibility Pattern

Role

The Chain of Responsibility pattern works with a list of Handler objects that have limitations on the nature of the requests they can deal with. If an object cannot handle a request, it passes it on to the next object in the chain. At the end of the chain, there can be either default or exceptional behavior.

Illustration

The Chain of Responsibility pattern is encountered frequently in real life. Imagine members of the public (such as you and me) approaching a bank, a government office, a hospital, or a similar institution with a request. In the case of a bank, we know that a first level of clerks will handle straightforward requests. Those requests requiring more inspection will be sent up to a supervisor, and ultimately the manager might have to authorize or rule on a request. The clerk, supervisor, and manager together are an example of a chain of responsibility. Of course, sometimes clerks can deal with matters quite well on their own, as illustrated in Figure 8-1.[8]

Chain of Responsibility pattern illustration—a bank clerk deals with a request

Figure 8-1. Chain of Responsibility pattern illustration—a ...

Get C# 3.0 Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.