O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example: Adding States and Identifying Users

The minimal Observer design pattern example had only a single state. You might think that adding additional states may be complicated, but you'll see that it's very easy. One of the key features about design patterns is that they're flexible, and adding states illustrates this flexibility.

Another issue that can be important in some applications using the Observer pattern is getting to know the observers. In the minimal example in the previous section, the observers were identified by their array index, which changes when observers are removed. However, using a property to identify observers is quite simple as well.

Multiple States

To understand multiple states, just imagine a daily newspaper that has several different sections. For example, an abbreviated list might include the following topic areas that regularly need to be changed:

  • News

  • Sports

  • Stocks

  • Entertainment

All these areas can be represented as String data, but for the sake of type variety, we will make Stocks a Number type. In the minimalist example, the initial placement of the state property is in the update() parameter. To have additional property states, all we need to do is add more properties. So, in the Observer interface, we'd just need to change the update() function to the following:

function update(news:String,sports:String,stocks:Number,entertainment:
    String):void;

That was easy. As you can see, different data types are not problematic at all.

Next, the concrete subject ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required