ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Minimalist Example of a Command Pattern | 251

The client assigns eachConcreteCommandinstance to specific triggers ininvokers.


Invokers are where the commands are called from. They hold on to the


ConcreteCommandobjects and call theirexecute( )methods when it’s time to execute


the command. You’ll clearly see how this is implemented in ActionScript 3.0 in the


minimalist application.


Minimalist Example of a Command Pattern


This example implements the command pattern class diagram in Figure 7-3. The


command pattern consists of thecommand interface,concrete commandsthat imple-


ment the command interface,invokersthat call theexecute( )method in concrete


commands,receiversthat implement the behavior required of commands, andclients


that create concrete commands and pass them on to invokers.


Code examples Examples 7-1 through 7-5 show the minimalist implementation of


the command pattern.


The Command Interface


Example 7-1 shows theICommandclass that defines the interface for commands. It


defines a single method calledexecute( ).


The Concrete Command


Example 7-2 shows theConcreteCommandclass that implements theICommandinter-


face. The parameterized constructor takes aReceiverclass instance and assigns it to


thereceiverproperty. Theexecute( )command is implemented by delegating to the


receiverinstance by calling itsaction( )method. Note that, because the receiver


Figure 7-3. Command pattern class diagram


Example 7-1. ICommand.as


package
{
public interface ICommand {
function execute( ):void;
}
}


Client

Receiver receiver
action()

ConcreteCommand
execute() receiver->action()

Command
execute()

Invoker
Free download pdf