I had been building variations of MVC without implementing the Observer pattern. Now having studied Observer, I realize how to do MVC properly.
MVC without Observer is like NATO without formal delegates. You’d let every citizen in every nation contact every other directly.
We want our model to publish to a group of subscribers. Flex bindings can pull this off. Consider, however:
- Encapsulation recommends, and modularization implies, that not every class directly accesses every other. (Beware of EventBroadcaster use & Singleton abuse.)
- While you have a handful of models with a slew of listeners, EventListeners are firing constantly, listening for changes. This will affect memory consumption.
Observer allows for an easily-readable, encapsulated, relationship between Model and View. Your model extends a Publisher class. Publisher has a function which adds subscribers to its subscriber Array. The Publisher class also has a function to notify its group of subscribers. The Publisher knows that any Object which subscribes will implement the Subscriber class.
Views which require Model/Publisher data extend a Subscriber class. The parent Subscriber is a good place to declare your static constant strings — these describe data groups. (Having these outside the model better supports encapsulation, but I’ve seen them in the model in other examples.) Each Subscriber child has an Array which notes the data groups the Subscriber is interested in. Each Subscriber has an update function which is called by the Publisher when it sends data.
A third class keeps things organized and readable. It’s there you subscribe the Views to their Model or Models.
I searched for similar AS3 Observer implementation examples online and found but one. Hope my example helps your code find its way.