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.

Example 4-35. Indifference.as<br />

package<br />

{<br />

public class Indifference extends Decorator<br />

{<br />

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

public function Indifference(<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( ) + "|Indifference";<br />

}<br />

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

{<br />

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

}<br />

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

{<br />

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

}<br />

}<br />

}<br />

At this point you can add your own concrete decorators. Use the same format as the<br />

others. You can also add additional concrete <strong>com</strong>ponents. So instead of just Dick<br />

and Jane, you can add others you’d like to decorate with good and evil.<br />

Implementing the Good and Evil Decorator<br />

Instead of a single implementation, you can try out two different implementations.<br />

The first one dresses up the two different concrete <strong>com</strong>ponents, and sends the results<br />

to the output window. The second takes the results, uses them to place movie clips<br />

on a “soul graph,” and adds a label to an angel or devil movie clip—depending on<br />

whether good or evil is predominant.<br />

Dual implementation<br />

The first implementation decorates both the Dick and Jane concrete <strong>com</strong>ponents.<br />

This one is set up to use all 14 deadly sins and heavenly virtues, but you can use any<br />

<strong>com</strong>bination you want. As each concrete <strong>com</strong>ponent (life and light) is wrapped,<br />

the good and evil properties are incremented or decremented, depending on which<br />

decorator wraps the concrete <strong>com</strong>ponent. The reference to the <strong>com</strong>ponents object in<br />

each of the decorators is a reference to the concrete <strong>com</strong>ponent being wrapped. With<br />

each new decorator, then, the value goes up and down depending on the wrapping<br />

decorator. Save Example 4-36 as MainDual.as in an <strong>ActionScript</strong> file. Open a new<br />

Flash document file, and save it as a Dual.fla. Type in MainDual in the Document<br />

160 | Chapter 4: Decorator Pattern

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

Saved successfully!

Ooh no, something went wrong!