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-21. Generic concrete decoration<br />

package<br />

{<br />

//Generic—NOT implemented<br />

public class GoodEvil extends Decorator<br />

{<br />

private var <strong>com</strong>ponents:Component;<br />

public function GoodEvil(<strong>com</strong>ponents:Component)<br />

{<br />

this.<strong>com</strong>ponents=<strong>com</strong>ponents;<br />

}<br />

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

{<br />

return <strong>com</strong>ponents.getSoul( ) + "|GoodEvil";<br />

}<br />

override public function good( ):Number<br />

{<br />

return +/-00 + <strong>com</strong>ponents.good( );<br />

}<br />

override public function evil( ):Number<br />

{<br />

return +/-00 + <strong>com</strong>ponents.evil( );<br />

}<br />

}<br />

}<br />

If you break down Example 4-21, the first part looks exactly like the previous examples.<br />

A Component instance, <strong>com</strong>ponents, is instantiated, and the constructor function<br />

wraps the <strong>com</strong>ponents object in itself:<br />

var <strong>com</strong>ponents:Component;<br />

public function GoodEvil(<strong>com</strong>ponents:Component)<br />

{<br />

this.<strong>com</strong>ponents=<strong>com</strong>ponents;<br />

}<br />

Next, the script re-implements the getter function, getSoul( ), to return both the current<br />

value of the concrete <strong>com</strong>ponent’s decorations plus its own decoration value.<br />

Again, this is what previous examples have done.<br />

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

{<br />

return <strong>com</strong>ponents.getSoul( ) + "|GoodEvil";<br />

}<br />

The next two functions add or subtract numeric values using the good( ) and evil( )<br />

methods. Each good adds to a good and subtracts from an evil, and vice versa for a<br />

vice—adds to evil and subtracts from good. So depending on the concrete decorator,<br />

you add or subtract from each of the two return values, and add that to the current<br />

value of the concrete <strong>com</strong>ponent.<br />

override public function good( ):Number<br />

{<br />

return +/-00 + <strong>com</strong>ponents.good( );<br />

152 | Chapter 4: Decorator Pattern

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

Saved successfully!

Ooh no, something went wrong!