ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Example: Adding States and Identifying Users | 297

duplicate=false;
for(var ob=0;ob<this.observers.length;ob++)
{
if(this.observers[ob]==obserNow)
{
duplicate=true;
trace( this.observers[ob].subName+ " already a subscriber.
\n");
}
}
if(! duplicate)
{
this.observers.push(obserNow);
}
}

In some cases, you may not want to limit single subscriptions with a common name.


To allow multiple subscribers with the same name, just don’t add the above code


that prevents doing so.


Updated Observer


Now the Observer application has far more functionality. Not only can each


observer be identified by a specific name, no more than a single observer with the


same name will be allowed to subscribe. What’s more, four states are now tracked


and sent out if changed. Example 8-10 through Example 8-13 should be saved in the


same folder using the captions for the filenames.


Example 8-10. Subject.as


package
{
//Subject Interface
public interface Subject
{
function subscribeObserver(o:Observer):void;
function unsubscribeObserver(o:Observer):void;
function notifyObserver( ):void;
}
}


Example 8-11. Observer.as


package
{
//Observer Interface
public interface Observer
{
function update(news:String,sports:String,stocks:Number,entertainment:
String):void;
}
}

Free download pdf