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.

update functions. The subscribe and unsubscribe functions are the same as previous<br />

Observer designs in this chapter. Save the classes in Example 8-15 and Example 8-16<br />

by their caption names.<br />

Example 8-15. Subject.as<br />

package<br />

{<br />

//Subject Interface<br />

public interface Subject<br />

{<br />

function subscribeObserver(o:Observer):void;<br />

function unsubscribeObserver(o:Observer):void;<br />

function notifyObserver(score:Number,damage:String):void;<br />

}<br />

}<br />

Example 8-16. Observer.as<br />

package<br />

{<br />

//Observer Interface<br />

public interface Observer<br />

{<br />

function update(score:Number,damage:String):void;<br />

}<br />

}<br />

Giving More Work to the Concrete Classes<br />

The two classes representing concrete subjects and observers are changed little from<br />

previous examples. The ConcreteSubject class still keeps track of who has subscribed<br />

and unsubscribed, and sends out notifications of state changes.<br />

The ConcreteObserver class, though, has taken on another responsibility. When a<br />

new ConcreteObserver is created, it makes sense to automatically subscribe her to the<br />

notification process. So instead of making it a two-step process, one to instantiate<br />

and another to subscribe, the ConcreteObserver now automatically subscribes new<br />

instances. To do this, only a single line had to be added to the constructor function:<br />

concreteObserver.subscribeObserver(this);<br />

The parameter references the instance being instantiated. At any time, the instance<br />

can unsubscribe or resubscribe by calling the unsubscribe or subscribe functions.<br />

Example 8-17 and Example 8-18 should be saved using the caption names.<br />

Dynamically Changing States | 303

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

Saved successfully!

Ooh no, something went wrong!