Skip to Content

The Observer Design Pattern in Actionscript 3

Author: Bill Trikojus and Anthony Kolber

“The primary objective of this pattern is to provide a way to handle runtime one-to-many relationships between objects in object oriented languages. The event from the observable object's point of view is called notification and the event from the observers' point of view is called update.”

The observer pattern is;

  • An event broadcasting system
  • Tells interested objects (observers) when something changes in another object (the subject)

The observer pattern is;

  • A design solution for linking classes and instances together
  • Two versions, Push & Pull
    •         Push (Instant Messaging)
    •         Pull (Email)

The push technique is built into flash as the EventDispatcher class



        When Pacman eats a power pellet, a message is broadcast to all enemies


  Abstract Example:

Overview of code required

// set up Pacman class to broadcast
Pacman extends EventDispatcher

// tell enemies to listen to pacman instance
pacman.addEventListener(Pacman.ATE_PELLET, enemy1.update);
pacman.addEventListener(Pacman.ATE_PELLET, enemy2.update);
pacman.addEventListener(Pacman.ATE_PELLET, enemy3.update);

// broadcast state change
pacman.dispatchEvent(new Event(Pacman.ATE_PELLET));
// enemies respond
enemyClass.update() {
    // run away

Download a basic Actionscript 3 Observer example that shows how to move an object around the stage using the keyboard. This example combines the Observer pattern with Model-View-Controller pattern.

For more on the Observer and other Design Patterns in Actionscript 3 check out 'Actionscript 3 Design Patterns' by William Sanders and Chandima Cumaranatunge.



Like this? Click a link below to share it...

Subscribe and Download

Subscribe to the Swinburne Faculty of Design Podcasts


2010-01-26: Ridha hela said:
thx for allowing me to download pacman's source code

2010-04-08: James said:
What if you had 100 enemies, do you have to type 'addEventListener' for all 100 enemies?

There must be an easier way to send the notification dynamically to all instances of the Enemy class.

If only I knew what it was.

2010-04-29: David said:
@James, you could have a global var on the model that is Bindable and is changed when the Pacman.ATE_PELLET Event is handled. Each Enemy would be bound to this global var. When the var changes the setter for this var on the enemy could call its update method. conversly when the pellet power runs out Pacman.PELLET_EXPIRED event could be dispatched, change the global variable again and the ghosts can chase pac again :)

2010-12-27: Abed Allateef said:
understand command design patterns using flash AS3 :

2011-06-01: john said:
Im trying to learn the observer pattern im dissapointed to find your Observer+mvc example link is dead.

Id really like to take a look at this file. Can sombody contact me or email me the file?

2011-06-01: Bill Trikojus said:
the download link has been fixed.

App to track my kids
App to track my kids

Post a comment

Garbage posts and SPAM will be deleted.

* Name:
* Email: (will not be made public)
* Comment:
* Reply Notification: