2

I have this situation:

An object that it's observable and another object that it is observer.

the observer has a method update(Observable obs,Object obj) that receive through notifyObservers the object changed.when observer receives the notify, update method prints the object changed. I want print out the result in a gui that implements MVC pattern.I'm following this guide MVC pattern. My idea is to make the Controller the observer. something like that:

public class Controller extends AbstractController implements Observer 
{
    public static final String TOTAL_HIT_COUNT_PROPERTY = "Total Hit";

    public void changeTotalHitCount(long new_total_hit_count)
    {
        setModelProperty(TOTAL_HIT_COUNT_PROPERTY, new_total_hit_count);
    }

    @Override
    public void update(Observable o, Object arg) 
    {

    }
}

but I don't know if it's a correct implementation!

th1rdey3
  • 4,176
  • 7
  • 30
  • 66
Mazzy
  • 13,354
  • 43
  • 126
  • 207

3 Answers3

8

Observer Pattern and MVC Pattern are 2 distinct design pattern - just to make sure we are on the same page.

In the MVC (at least by definition) pattern, the View is supposedly automatically updated when the Model changes, I am guessing this is what you are trying to do. In that situation, that means your Observer should be the View, not the Controller, and your Model will be your Observable object.

Thus:

Observable changes --> update Observer

will sort of replicate what you are trying to get in the pure MVC pattern:

Model changes --> update View

I am not saying this should be the way to do things, but I would think if you try to apply the Java's Observer/Observable to the MVC pattern, this could be on way to go with.

TS-
  • 4,311
  • 8
  • 40
  • 52
  • ok so my observable object became MODEL in mvc pattern and the view is the observer.right?but in this case who is the controller? – Mazzy Jul 09 '12 at 20:22
  • This [example](http://stackoverflow.com/a/3072979/230513), which uses `Observer` and `Observable`, shows the relationship between the two patterns. – trashgod Jul 09 '12 at 20:26
  • 2
    Controller sits in the middle, so if any action happens in the View, you need to call the controller. The controller then decides what to do (eg. apply business logic, do other funky stuff, and possibly update the Model as well) – TS- Jul 09 '12 at 20:29
  • But my application is very simple...it doesn't receive any input from the user but only input from the observable object so does it have sense to have a controller? – Mazzy Jul 09 '12 at 20:31
  • 3
    So if it's super simple, may be you don't want to apply the MVC pattern at all and just do the Observable/Observer part of it. Sometimes design pattern can actually be overkill. – TS- Jul 09 '12 at 20:32
  • But suppose my application has input users, the controller will be observer or observable? – Mazzy Jul 09 '12 at 20:39
  • In a very very very broad way ... you then have your View to be Observable and the Controller to be the Observer of that. This really is if you are for whatever reason dying to apply Observer/Observable pattern to MVC. As a side note, this idea is not a good way to go down. MVC is just not meant to be applied that way. You probably want to read up more on the MVC pattern to have better understanding. – TS- Jul 09 '12 at 20:42
  • well I have to open another question to explain my case study – Mazzy Jul 09 '12 at 20:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13634/discussion-between-tsoverflow-and-mazzy) – TS- Jul 09 '12 at 20:47
  • You can take a look at this example: http://www.andypatterns.com/index.php/blog/puremvc_refactor/refactoring_to_puremvc_-_java_version/ which refactors a very simple Java Swing-application to using PureMVC (which is an MVC framework). – Simon Forsberg Aug 31 '12 at 13:40
5

Observer Pattern is part of MVC. It is an integral part of MVC.

The Observer pattern is also a key part in the familiar model–view–controller (MVC) architectural pattern. Source: https://en.wikipedia.org/wiki/Observer_pattern

The MVC pattern can be seen as a combination of these three design patterns: Observer, Composite, and Strategy Patterns. This means Observer and MVC are compatible.

Joel Karunungan
  • 312
  • 3
  • 12
1

In the book "Design Patterns - Elements of Reusable Design" (by the infamous gang of four), section 1.2 (Design Patterns in SmallTalk MVC), it mentions in several places about how MVC uses the Observer Pattern, I quote below :

Taken at face value this example reflects a design that decouples views from models. But the design is applicable to a more general problem: decoupling objects so that changes to one can affect any number of others without requiring the changed object to know the details of the others. This more general design is described by the Observer design pattern

And in the same section later

But the main relationships in MVC are given by Observer, Composite and Strategy design Patterns.

revolutionary
  • 3,314
  • 4
  • 36
  • 53