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