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.

another kind of object that can be decorated, such as a front yard to be decorated<br />

with gnomes and pink plastic flamingoes. Additionally, you want to be able to<br />

retrieve information, and so it has a getter function.<br />

Abstract Decorator Class<br />

Next, the abstract decorator is a subclass class of the Component class that inherits the<br />

information variable, so nothing is needed as far as the information variable is concerned.<br />

In fact, nothing’s required for this simple example other than defining the<br />

class as an extension of the Component class. However, the getInformation() method<br />

is re-implemented independently of the Component class, using the override statement—which<br />

does what it says on the tin; it overrides parent class methods. This is<br />

done to distinguish the same method being used for the Decorator class from the<br />

method being used for the Component class. All the concrete decorations are subclassed<br />

from the Decorator class, and all concrete <strong>com</strong>ponents are subclassed directly<br />

from the Component class. Further on, the concrete <strong>com</strong>ponents will be wrapped in<br />

concrete decorators, and such distinctions be<strong>com</strong>e important in more <strong>com</strong>plex<br />

implementations of the Decorator design pattern. The trace( ) statement is used to<br />

show you where in the process the abstract Decorator class appears. Save<br />

Example 4-2 as Decorator.as.<br />

Example 4-2. Decorator.as<br />

package<br />

{<br />

//Abstract Decorator in Decorator <strong>Design</strong> Pattern<br />

//**************<br />

//Abstract class<br />

public class Decorator extends Component<br />

{<br />

trace("|*|Decorator|*|");<br />

override public function getInformation( ):String<br />

{<br />

return information;<br />

}<br />

}<br />

}<br />

Once the two abstract classes, Component and Decorator, have been established, it’s<br />

time to work with the concrete classes. For this example, only a single concrete <strong>com</strong>ponent<br />

is created. Cleverly named ConcreteComponent, this class represents whatever<br />

will be decorated in a Decorator design pattern. You can have multiple concrete<br />

<strong>com</strong>ponents that all use the same set of decorations, or only a single one. Later in<br />

this chapter, you will see an application where multiple concrete classes are decorated<br />

by a single set of decorators. The nice thing about the Decorator is that you can<br />

add as many concrete <strong>com</strong>ponents as you want. Imagine a business web site where<br />

the concrete <strong>com</strong>ponent represents an e-business site that you’ve worked on for sev-<br />

136 | Chapter 4: Decorator Pattern

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

Saved successfully!

Ooh no, something went wrong!