• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MVC and Observer Pattern

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone please explain the implementation of observer pattern in MVC?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Model is the one that implements the observer pattern so it can notify interested objects when the state changes.
Using the observer keeps the model decoupled from views and controllers.

Hope this helps,
Drazen.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object-oriented design patterns such as Model-View-Controller specify a "design" strategy which can be applied to a particular problem or concern. However, design patterns (1) do not specify any type of "implementation" detail, (2) do not specify any specific software product or programming language, and (3) can be implemented with a variety of object/class structures.

So, due to number three above, there is no accurate answer for Maya's question
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Drazen Grabovac wrote:
Model is the one that implements the observer pattern so it can notify interested objects when the state changes.
Using the observer keeps the model decoupled from views and controllers.


Sort of. I think you understand but just to clarify in MVC, View is the Observer (Listener), Model is the Subject.

A View can register itself to the model it is interested to observe/listen using Model.registerListener(itself<View). After that when a model is changed the model calls Observer.notify(itself<Model) for all the registered Observers, the signature might vary, the idea is to notify all the Observers.

Note that you will not use this pattern much in Web MVC, but in Desktop applications, this pattern is very common.

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I beg to differ in as much as that the Observer pattern cannot be generalized: I feel that the Observer pattern, as originally designed by the GoF, is makes use of both asynchronous and synchronous messaging. I say this because originally, one needs to receive() messages (synchronously) after being notify()ed (asynchronously). Hence, the views do not actually listen.

However, it is possible to use onMessage(), which is a listener behavior, as well.

Hence, with the JEE/EJB server, one can actually separate the concerns of listening from the core by aggregating either receive() or onMessage() that would be implemented by the server. Thus Pub-Sub works for all, with no different for HTTP subscribers/providers.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic