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.”
http://en.wikipedia.org/wiki/Observer_design_pattern
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
Example:
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.
Share
Like this? Click a link below to share it...Subscribe and Download

Comments
2010-01-26: Ridha hela said:thx for allowing me to download pacman's source code
Post a comment
Garbage posts and SPAM will be deleted.
