Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

To implement the Chain of Responsibility, you need:

Handler – The interface that defines the method used to pass a message to the next handler. That message is normally just the method call, but if more data needs to be encapsulated, an object can be passed as well.

ConcreteHandler – A class that implements the Handler interface. It keeps a reference to the next Handler instance inline. This reference is either set in the constructor of the class or through a setter method. The implementation of the handleMessage method can determine how to handle the method and call a handleMethod, forward the message to the next Handler or a combination of both.

Benefits and Drawbacks

Chain of Responsibility offers great flexibility in event processing for an application, since it manages complex event handling by dividing the responsibilities among a number of simpler elements. It allows a set of classes to behave as a whole, since events produced in one class can be sent on to other handler classes within the composite.

Of course, the flexibility that this pattern provides comes with a price; the Chain of Responsibility becomes difficult to develop, test and debug. As the forwarding chain becomes more complex, you have to carefully monitor whether events are being properly forwarded.

Failure to plan for the different forwarding possibilities can result in dropped messages (messages that have no handler and so never have a response) or communication “chatter.” Chatter refers to a high volume of messages and multiple forwarding stages in the chain. If many messages are produced during a short period of time and they are passed along several times before they are handled, the system might slow down.

Pattern Variants

There are many ways to adapt Chain of Responsibility to suit application requirements. The two considerations are handling strategies and forwarding strategies.

Handling strategies focus on exactly how handler behavior is implemented. Some of the possible variants include:

Default handler – Some implementations set up a base handler, which becomes the default for the chain. It is normally used only when there is no explicitly defined forwarding class. A default handler is especially helpful in avoiding the problem of dropped messages previously mentioned in the Benefits and Drawbacks section for this pattern.

Handle and extend – In this variant, event handling involves adding to a base behavior as the event is propagated along the chain. This is often helpful for activities such as logging.

Dynamic handlers – Some Chain of Responsibility implementations allow the message forwarding structure to be changed at runtime. By defining a setter method for each class of the chain, you can define and modify the chain as it is used in the application (with all of the resulting complexity that involves).

Forwarding strategies define various approaches to handle or forward messages produced by a component:

Handle by default – Handle any message that is not specifically forwarded.

Propagate by default – Forward any message that is not explicitly handled.

37