Java Reference
In-Depth Information
Messages must be handled by one of several objects within the system.
Messages follow the “handle or forward” model—that is, some events can be handled at the level where they are
received or produced, while others must be forwarded to some other object.
Description
When some action takes place in an object-oriented system, it is often represented by an event or a message. Such
a message may take the form of a method that will be called, or it may be an object within the system. Typically,
the message will be directed to another object that can respond to or handle the message.
In the simplest cases, the same object that produces a message also responds to it. For instance, a text field might
produce events in response to user action (such as typing on a keyboard), and also respond to those events
(displaying text in the field).
In more complex cases, responding to messages can be more involved. A message requesting a change in the
appearance or layout of a GUI component might be dealt with at different levels. If the request is to change the
alignment of text within a field, the component itself might respond. A request to change the alignment of the
entire text field would probably have to be directed to some higher-level organizing object; perhaps a panel or
frame containing the text field. This kind of model is appropriate for the Chain of Responsibility pattern.
The Chain of Responsibility is a referral chain for messages. If an object cannot handle a given message, it passes
the message on to some other object. Frequently, the Chain of Responsibility is implemented with a parent-child
or container-contained model. With this approach, messages not handled by a child object are sent to the parent,
and potentially the parent's parent, until a suitable handler object is reached.
The Chain of Responsibility is well-suited for a variety of object-oriented GUI activities. GUI help functions,
component layout, formatting, and positioning all might use this pattern. In business models, the pattern is
sometimes used with whole-part models. For example, a line item on an order might send a message to the order
it's on—the order composite—for action.
For a real-world example of Chain of Responsibility, consider a travel request within a company. Typically, such
a request will be propagated upward to the appropriate manager. Therefore, a request to travel to the grocery store
for more coffee might only require approval from your manager, whereas a request to travel to Kansas could rise
through an organizational hierarchy until it finally reached an individual with approval authority (perhaps the
great and powerful Oz).
The Chain of Responsibility sequence diagram is shown in Figure 2.1 .
Figure 2.1. Chain of Responsibility sequence diagram
Implementation
The class diagram for Chain of Responsibility is shown in Figure 2.2
Figure 2.2. Chain of Responsibility class diagram
 
 
Search WWH ::




Custom Search