Design Patterns Java™ Workbook

(Michael S) #1
Chapter 12. Chain of Responsibility

Chain of Responsibility without Composite...........................................................................................


CHAIN OF RESPONSIBILITY requires a strategy for ordering the search for an object that can
handle a request. Usually, the order to follow will depend on an underlying aspect in
the modeled domain. This frequently occurs when there is composition, as in the Oozinoz
machine hierarchy. However, this pattern can apply outside object models that are
composites.


CHALLENGE 12.5


Cite an example when the CHAIN OF RESPONSIBILITY pattern might occur if
the chained objects do not form a composite.

Summary......................................................................................................................................................


When you apply the CHAIN OF RESPONSIBILITY pattern, you relieve a client from having to
know which object in a collection supports a given behavior. By setting up the search for
responsibility to occur across objects, you decouple the client from any specific object in the
chain.


The CHAIN OF RESPONSIBILITY pattern occurs occasionally when an arbitrary chain of
objects can apply a series of different strategies to tackling a problem, such as parsing a user's
input. More frequently this pattern occurs in aggregations, in which a containment hierarchy
provides a natural ordering for a chain of objects.CHAIN OF RESPONSIBILITY leads to simpler
code in both the hierarchy and the client, which is the main advantage of this pattern.

Free download pdf