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

Command

Also known as Action, Transaction

Pattern Properties

Type: Behavioral

Level: Object

Purpose

To wrap a command in an object so that it can be stored, passed into methods, and returned like any other object.

Introduction

When a user selects an action to be performed, the application needs to know where to get the relevant data and behavior. Normally, the application knows the number of options a user has and will keep the logic in a central place (hardcoded). When an option is selected, the application looks up what to do, assembles the data required, and invokes the necessary methods.

Of course, you are perfect (most programmers are), but your application is intended for normal users and they sometimes make mistakes. That’s why many current applications allow users to undo every task back up to a certain checkpoint, such as the last time the user saved.

Imagine doing that in your application with its current design. It means creating a history list—a list of all the actions the user has performed, all the data that was required for the action, and the previous state. After about three or four actions, the history list will be bigger than the entire application, because of all the redundant data.

It makes more sense to combine the user's action into one object: the Command object. This contains the behavior and the data required for one specific action. Now an application just invokes the execute method on the

Command object to execute the command. The application no longer needs to know all the available options and

can be easily changed to include more user actions.

Y

 

 

 

L

Applicability

 

 

F

 

 

M

 

 

 

Use the Command pattern to:

 

A

E

 

 

 

 

T

 

 

Support undo, logging, and/or transactions.

 

 

Queue and execute commands at different times.

Decouple the source of the request from the object that fulfills the request.

Description

An application that doesn't use the Command pattern would have to provide a method in its handler class for each appropriate event that may occur. That means the handler needs to have all the information to be able to execute the action. Introducing new actions would require adding new methods to the handler class.

The Command pattern encapsulates both the data and functionality required to fulfill a specific action or request. It provides a separation between when an action needs to be taken and how it needs to be executed.

An application that uses the Command pattern creates a source (for instance, a GUI), a receiver (the object that carries out part of the request), and the command (Listener). The command receives the reference to the receiver and the source receives a reference to the command . In this example, when the user clicks the button in the GUI, the execute or listener method on a command object is created (see Figure 2.3).

Figure 2.3. Sequence diagram for invocation of Command

TEAM FLY PRESENTS

41