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

The command object is sent to the invoker, which implements the Command interface. In its simplest form, the interface has an execute method. The implementing classes store the receiver as an instance variable. When the execute method is called, the Command calls the doAction method on the Receiver. The Command can call several methods on the Receiver.

Implementation

The Command class diagram is shown in Figure 2.4.

Figure 2.4. Command class diagram

To implement the Command pattern, you need the following:

Command –

Invoker –

Receiver –

The interface that defines the methods for the Invoker to use.

The invoker of the execute method of the Command object.

The target of the Command and the object that fulfills the request; it has all the information needed.

ConcreteCommand – Implementation of the Command interface. It keeps a reference to the intended Receiver. When the execute method is called, ConcreteCommand will call one or more methods on the Receiver.

When implementing the Command pattern, you need to make some choices concerning the handling of calls. Do one of the following:

The class that implements the Command interface can just be a coupling between the invoker and the receiver, and forward all the calls directly. This makes the ConcreteCommand lightweight.

The ConcreteCommand can be the receiver and handle all the requests itself. This is most appropriate when there is no specific receiver for that request.

42