10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Client<br />

Figure 7-3. Command pattern class diagram<br />

The client assigns each ConcreteCommand instance to specific triggers in invokers.<br />

Invokers are where the <strong>com</strong>mands are called from. They hold on to the<br />

ConcreteCommand objects and call their execute( ) methods when it’s time to execute<br />

the <strong>com</strong>mand. You’ll clearly see how this is implemented in <strong>ActionScript</strong> <strong>3.0</strong> in the<br />

minimalist application.<br />

Minimalist Example of a Command Pattern<br />

This example implements the <strong>com</strong>mand pattern class diagram in Figure 7-3. The<br />

<strong>com</strong>mand pattern consists of the <strong>com</strong>mand interface, concrete <strong>com</strong>mands that implement<br />

the <strong>com</strong>mand interface, invokers that call the execute( ) method in concrete<br />

<strong>com</strong>mands, receivers that implement the behavior required of <strong>com</strong>mands, and clients<br />

that create concrete <strong>com</strong>mands and pass them on to invokers.<br />

Code examples Examples 7-1 through 7-5 show the minimalist implementation of<br />

the <strong>com</strong>mand pattern.<br />

The Command Interface<br />

Example 7-1 shows the ICommand class that defines the interface for <strong>com</strong>mands. It<br />

defines a single method called execute( ).<br />

Example 7-1. ICommand.as<br />

The Concrete Command<br />

Invoker<br />

Receiver<br />

action()<br />

package<br />

{<br />

public interface ICommand {<br />

function execute( ):void;<br />

}<br />

}<br />

receiver<br />

Command<br />

execute()<br />

ConcreteCommand<br />

execute()<br />

receiver->action()<br />

Example 7-2 shows the ConcreteCommand class that implements the ICommand interface.<br />

The parameterized constructor takes a Receiver class instance and assigns it to<br />

the receiver property. The execute( ) <strong>com</strong>mand is implemented by delegating to the<br />

receiver instance by calling its action( ) method. Note that, because the receiver<br />

Minimalist Example of a Command Pattern | 251

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!