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.

Example 4-6. DecTest.as<br />

package<br />

{<br />

import flash.display.Sprite;<br />

public class DecTest extends Sprite<br />

{<br />

public function DecTest( )<br />

{<br />

//Instantiate Concrete Component<br />

var testComponent:Component = new ConcreteComponent( );<br />

//Wrap first decorator around <strong>com</strong>ponent<br />

testComponent=new DecConA(testComponent);<br />

//Wrap second decorator around <strong>com</strong>ponent<br />

testComponent=new DecConB(testComponent);<br />

//Output results<br />

trace(testComponent.getInformation( ));<br />

}<br />

}<br />

}<br />

Figure 4-5 shows what you should see in your Output window when you test the<br />

movie.<br />

Figure 4-5. Decorations on <strong>com</strong>ponent<br />

To understand what’s going on in the Decorator pattern, go back and look at<br />

Figure 4-2. The example application first instantiated a ConcreteComponent( ) object.<br />

That object displays a message pointing to its decorations. Imagine that object<br />

(testComponent) as the smallest can to the far left in Figure 4-2. That can is then<br />

placed into decorator Can #1. At this point, the concrete <strong>com</strong>ponent object<br />

(testComponent) is decorated with Can #1, but retains its original properties –much<br />

in the same way that a lawn decorated with a family of gnomes still retains its property<br />

of green grass. Next, Can #1, which now contains the concrete <strong>com</strong>ponent, is<br />

dropped into Can #2. Now Can #2 has both Can #1 and the Concrete <strong>com</strong>ponent<br />

Can. Thus, Can #2 has all of the properties of itself plus those of the cans inside.<br />

In a sense, the whole process works like the <strong>com</strong>pound operator, plus-equal (+=).<br />

Each decorator attaches itself to the existing <strong>com</strong>ponent and its decorator. As each<br />

decorator is added, all the previous ones are retained but not duplicated, unless you<br />

add the same decorator more than once. So, as the output shows, you can add as<br />

many decorations as you want simply by wrapping them in the existing object in one<br />

of the decorators.<br />

140 | Chapter 4: Decorator Pattern

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

Saved successfully!

Ooh no, something went wrong!