PureMVC Architects Lounge

Announcements and General Discussion => Architecture => Topic started by: skasssam on June 13, 2008, 08:19:34

Title: What is the purpose of the Observer?
Post by: skasssam on June 13, 2008, 08:19:34
Now that I am waist deep in my app I have a better understanding of PureMVC (mediators, proxies, notifications, etc...)

But where do observers fit into the big picture?

Anyone can provide insight?


Title: Re: What is the purpose of the Observer?
Post by: puremvc on June 13, 2008, 02:17:54
The Observer class is internal to the framework's notification system.

The View class maintains a map of observer lists; an associative array where the key is the notification name and the value is an array.

The observer list contains Observer instances, one for each mediator or command to be sent notifications by that name.

The observer has a reference to an instance of some class to notify (the 'notify context') and the method to call.

In the case of a mediator, when it is registered, it is queried as to its notification interests. For each notification name returned, an observer is created with that mediator as the context and 'handleNotification' as the 'notify method'. The observer is added to the list for the notification name in question.

In the case of commands, it is a little different. The command isn't actually instantiated until it is needed; when the notification it is registered for is sent, the command must be created and have the notification passed to its execute method.

This instantiation and execution of the command is the job of the controller. So when a command is registered, the observer points not to the command instance (which doesn't exist yet) but instead to the controller. The notify method is executeCommand. So the observer instance for all commands actually points to the same instance and method, which determines from the notification name which command class to instantiate and execute.


Title: Re: What is the purpose of the Observer?
Post by: skasssam on June 19, 2008, 12:44:24
Thanks for the explaination. BTW, PureMVC has helped me alot building my Flex App. Thank You!