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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Applying a Simple Decorator Pattern in Flash:<br />

Paper Doll<br />

You can move from a conceptual example to a concrete one with a <strong>com</strong>puterized<br />

paper doll game. The paper doll <strong>com</strong>ponent is the doll being dressed, and the decorations<br />

are clothing items selected for the doll. Using <strong>ActionScript</strong>’s new Loader class,<br />

different GIF files are used to decorate a base GIF image of a Victorian paper doll. As<br />

in the previous abstract example, this example uses a single concrete <strong>com</strong>ponent and<br />

several decorations.<br />

This concrete example does not have an interface—keeping the focus on the application’s<br />

use of a Decorator pattern. However, it does have a graphic output so you can<br />

see the pattern’s ultimate output. (Later in this chapter, there’s an example with a<br />

full interface.)<br />

Setting Up the Component Class<br />

The first class is the <strong>com</strong>ponent class, Model. From this, all other classes are subclasses.<br />

It’s very simple but key to the success of the application. As you can see, it’s<br />

very close to the previous abstract Decorator. It consists of a method, getDressed( ),<br />

and a string variable, whatToWear, as shown in Example 4-7. Save the file as Model.as.<br />

Example 4-7. Model.as<br />

package<br />

{<br />

//Abstract class<br />

public class Model<br />

{<br />

protected var whatToWear:String;<br />

public function getDressed( ):String<br />

{<br />

return whatToWear;<br />

}<br />

}<br />

}<br />

Keep in mind that Example 4-7 should be treated as an abstract class. So the method<br />

getDressed( ) needs to be created as an override public function for it to work the<br />

way we want. However, the property whatToWear doesn’t need to be changed from its<br />

inherited characteristics.<br />

Decorator Class: Dressing the Dolls<br />

Example 4-8 is the abstract Decorator class, Dresser, which extends the <strong>com</strong>ponent<br />

class, Model. The major contribution here is simply re-implementing the getDressed( )<br />

method with a reference to the inherited whatToWear property.<br />

Applying a Simple Decorator Pattern in Flash: Paper Doll | 141

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

Saved successfully!

Ooh no, something went wrong!