1
votes

I have a java application that implements the observer/observable pattern. In my MVC model, the observer class is the View, and the observable class is the controller.

Every changes in my observable class are sent to the observer one using setChanged(); and notifyObservers(); instructions, and the observer class is changed using the update method.

This works allright, the problem is that during the execution, sometimes, the user has to interact with the application using some JButtons defined in the observer class, but I don't know how to send this information to the observable class so that this continues its execution considering the user interaction. in fact, I don't even know if it is possible to do it.

Any help will be appreciated.

4
The observer shall act as an event source, and sends a message on observable like any other event-source. - Amir Pashazadeh

4 Answers

1
votes

In the observer pattern, you do not send data to the observable (which is usually the model and not the controller, but go with what works for you).

What you typically do is define some operations on your model or some commands on your controller you can call (in an interface, for loose coupling). Clicking the button would then call one of these functions, which in turn might modify whatever you like. That change is then of course reported to all observers, which can adapt.

1
votes

Generally, the observer pattern is used to loosely couple communication via two components in one direction.

In your case, the observable knows that some other object my observe him and thus call its notifyObservers() method.

However, the other way around your observer must (directly or indirectly) know the observable (as it has to register/unregister) itself to become notified. Thus, there should be a reference from your observer to your observable. Thus, you should be able to handle UI events in the observer and cause some delegation to a public (or protected) visible method within your observable.

0
votes

Actually, if your View is also a Controller, well... you have TWO Controllers :) What you could do is actually implementing the MVC pattern you mentioned:

+--------------------------------------+
|              +--------------------+  |
|          +---<     Controller     |  | <- Your current "Observable"
|          |   +--------------------+  |
|          |                           |
|    +-----v-----+                     |
|    |   Model   <-------+             |
|    +-----v-----+       |             |
|          |             |             |
|          |             |             |
|    +-----v-------------^----------+  |
|    |  View   +     Controller     |  | <- Your current "Observer"
|    +------------------------------+  |
|                                      |
+---------- Your application ----------+

So your current Observer, in the MVC pattern, would be both a View and a Controller. Now, you want to have a unique Model that will be the Observer of your two Controllers (that will still be Observable), and all your View will keep being an Observer, but will now be connected to your unique Model.

That Model I'm talking about will simply hold the application-data that you are either:

  • manipulating with the Controllers
  • showing with the Views

You can also make it basically represents the internal state of application. I think that this is what you're looking for here. The MVC pattern will make you gain a lot in flexibility :)
You can read more about it there: MVC Pattern - tutorialspoint.

0
votes

Observer can interact with Observable in its update method using reference to Observable:

void update(Observable o, Object arg)

This method is called whenever the observed object is changed. An application calls an Observable object's notifyObservers method to have all the object's observers notified of the change.

Parameters:

  • o - the observable object.
  • arg - an argument passed to the notifyObservers method.