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.

this all work right is the use of an array to hold the subscribing observers, and a distribution<br />

method for broadcasting the current state.<br />

Subject Interface<br />

All we need for the Subject interface are three methods to take care of the subscription<br />

and notification work. Because all of the functions in an interface construct are<br />

abstract, we don’t have a lot of detail to address. However, we’ve got to be careful to<br />

be sure that all the necessary parts are in place. Example 8-5 shows the script to be<br />

saved as Subject.as:<br />

Example 8-5. 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( ):void;<br />

}<br />

}<br />

In the first two functions, you’ll see evidence of <strong>com</strong>position. The “o” parameter is an<br />

Observer datatype, which is a reference to another interface that’ll be built as part of<br />

the Observer design pattern. If you look at the diagram in Figure 8-2, you’ll see an<br />

arrow from the abstract Subject to the abstract Observer. That reference to the<br />

Observer datatype is part of the process that uses <strong>com</strong>position rather than<br />

inheritance.<br />

Observer Interface<br />

The Observer interface is deceptively simple. The single update( ) function is actually<br />

part of the <strong>com</strong>position between the Observer and Subject structures. However, we<br />

see the <strong>com</strong>position only when we look at the connection between the concrete<br />

observer and subject through the update( ) supertype in the Observer interface. Save<br />

the code in Example 8-6 as Observer.as:<br />

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

package<br />

{<br />

//Observer Interface<br />

public interface Observer<br />

{<br />

function update(light:String):void;<br />

}<br />

}<br />

290 | Chapter 8: Observer Pattern

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

Saved successfully!

Ooh no, something went wrong!