Understanding the Command Pattern


In its purest form, the Command pattern consists of six elements.

  • Command interface

  • Concrete command

  • Receiver

  • Client

  • Invoker

The following sections look at these elements in detail.

The Interface

In the simplest form, the command interface defines just one method, often called execute() by convention. The execute() method is responsible for running the requested operation. That means that in the simplest form, the interface looks like this:

package com.peachpit.aas3wdp.commands {    public interface ICommand {       function execute():void;    } }


The command interface is essential to the Command pattern because it allows all implementing command types to use the same interface even though they have different implementations. That way, commands can be called without the calling code having to know much (if anything) about the specific command. Just like you can flip a switch to turn on and off an electrical device without having to know the details of how the particular electrical device operates, the command interface provides a consistent way to operate programmatic objects that might have disparate modes of operation behind the scenes.

This ICommand interface is all that is necessary for a basic command. However, sometimes we want command classes to support the possibility of undoable and redoable commands. For that reason, we can define two subtypes called IUndoableCommand and IRedoableCommand. Here's IUndoableCommand.

package com.peachpit.aas3wdp.commands {    public interface IUndoableCommand extends ICommand {       function undo():void;    } }


And now here's the IRedoableCommand interface:

package com.peachpit.aas3wdp.commands {    public interface IRedoableCommand extends ICommand {       function redo():void;    } }


You'll notice that both of these interfaces extend ICommand, meaning that all IUndoableCommand and IRedoableCommand implementing classes also pass the test as implementing ICommand.

We'll start by looking at examples that implement just the ICommand interface. Later in the chapter, we'll look at a sample application that uses IUndoableCommand and IRedoableCommand.

The Concrete Command and Receiver

The concrete command is the class that implements the interface in a useful way by defining the execute() method so that it actually runs an operation.

The concrete command usually requires a receiver, which is the object that is the target of the operation. Although it's never strictly necessary, in many cases, the receiver reference is passed to the concrete command constructor. The following is an example of a concrete command that rotates a display object clockwise. In this case, the display object is the receiver.

package com.peachpit.aas3wdp.commandexample {      import com.peachpit.aas3wdp.commands.ICommand;    public class RotateClockwiseCommand implements ICommand {       private var _reveiver:DisplayObject;       public function RotateClockwiseCommand(receiver:DisplayObject) {          _receiver = receiver;       }       public function execute():void {          _receiver.rotation += 20;       }    } }


The Client and Invoker

The client is the object that instantiates the command object, and the invoker is the object that calls the execute() method of the command object. The client might be the main class of an application, and the invoker might be a button. There are many possible scenarios, and as you'll see throughout the examples in this chapter, there are no definitive rules for what types of objects can be clients and invokers. In fact, in some cases the client and invoker might be the same object.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net