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

A MacroCommand contains a list of subcommands. When the execute method is called, the MacroCommand forwards subcommands.

If the MacroCommand supports undo, all internal commands must support it as well. When undo is called, this call must be forwarded to the children in the reverse order of the execute method.

Related Patterns

Related patterns include the following:

Composite (page 157) – Use the Composite pattern to implement MacroCommands.

Memento (page 88) – Keeps the state of the receiver within the command to support undoing a Command.

Prototype (page 28) – The Prototype pattern can be used to copy the command before placing it in the history list.

Singleton (page 34) – In most applications, the history list is implemented as a Singleton.

Example

Note:

For a full working example of this code example, with additional supporting classes and/or a RunPattern class, see “ Command ” on page 374 of the “ Full Code Examples ” appendix.

In the Personal Information Manager, users might want to update or modify information in their system. This code demonstrates how the Command pattern can provide update and undo behavior for a location.

In this example, a pair of interfaces model the generic command behavior. The basic command action is defined by the execute method in Command, while UndoableCommand extends this interface by adding undo and redo methods.

Example 2.4 Command.java

1.public interface Command{

2.public void execute();

3.}

Example 2.5 UndoableCommand.java

1.public interface UndoableCommand extends Command{

2.public void undo();

44

3.public void redo();

4.}

In the PIM, the location of an appointment will be used to implement an undoable command. An appointment stores a description of an event, the people involved, the location, and the start and end time(s).

Example 2.6 Appointment.java

1.import java.util.Date;

2.public class Appointment{

3.private String reason;

4.private Contact[] contacts;

5.private Location location;

6.private Date startDate;

7.private Date endDate;

8.

9.public Appointment(String reason, Contact[] contacts, Location location, Date startDate,

Date endDate){

10.this.reason = reason;

11.this.contacts = contacts;

12.this.location = location;

13.this.startDate = startDate;

14.this.endDate = endDate;

15.}

16.

17.public String getReason(){ return reason; }

18.public Contact[] getContacts(){ return contacts; }

19.public Location getLocation(){ return location; }

20.public Date getStartDate(){ return startDate; }

21.public Date getEndDate(){ return endDate; }

22.

23. public void setLocation(Location location){ this.location = location; }

24.

25.public String toString(){

26.

return "Appointment:" + "\n

Reason: " + reason +

27.

"\n

Location: " + location + "\n

Start: " +

28.

startDate + "\n End: " + endDate + "\n";

29.}

30.}

The class ChangeLocationCommand implements the UndoableCommand interface and provides the behavior required to change the location for an appointment.

Example 2.7 ChangeLocationCommand.java

1.public class ChangeLocationCommand implements UndoableCommand{

2.private Appointment appointment;

3.private Location oldLocation;

4.private Location newLocation;

5.private LocationEditor editor;

6.

7. public Appointment getAppointment(){ return appointment; }

8.

9.public void setAppointment(Appointment appointment){ this.appointment = appointment; }

10.public void setLocationEditor(LocationEditor locationEditor){ editor = locationEditor; }

11.

12.public void execute(){

13.oldLocation = appointment.getLocation();

14.newLocation = editor.getNewLocation();

15.appointment.setLocation(newLocation);

16.}

17.public void undo(){

18.appointment.setLocation(oldLocation);

19.}

20.public void redo(){

21.appointment.setLocation(newLocation);

22.}

23.}

The class provides the ability to change a location using the execute method. It provides undo behavior by storing the previous value of the location and allowing a user to restore that value by calling the undo method. Finally, it supports a redo method that enables users to restore the new location, if they happen to be very indecisive.

45