9

This question pertains to MVC (Model-View-Controller). My Model currently updates my View when it has changed using the Observer / Observable pattern in Java:

public class Model extends Observable {
}

public class View implements Observer {

    @Override
    public void update(observable o, Object obj) {
        // ... update the view using the model.
    }

}

This works fine. However, my model is growing more complex - it's starting to hold Lists of other classes:

public class Model extends Observable {
    List<Person> people = new ArrayList<Person>();
}

public class Person {
    private String name = "";
    // ... getter / setter for name
}

My problem is: when a Person's name changes I want to update the view listening to the model which contains that person. The only way I can think of is to have Model implement an Observer class and have Person extend an Observable class. Then, when Person changes, he notifies his observers (which would include the parent Model).

However, this seems like a lot of work if my models get complex. Are there any better ways to "bubble-up" changes to the parent model?

sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • 1
    Actually, it's bad design to be using MVC. – Hot Licks Nov 04 '12 at 21:54
  • Can you substantiate that a bit or offer an alternative? – sdasdadas Nov 05 '12 at 16:32
  • Basically, MVC does not "fit" standard "modern" UI concepts. You have to stand on your head to "separate" the design into those three artificial categories. You do want to maintain a data/presentation separation as much as possible, but when you attempt to separate the "view" and the "controller" you have to start making meaningless distinctions. – Hot Licks Nov 05 '12 at 16:45
  • I have to disagree - over the last few weeks I've found more and more that I need to separate logic from my data layer. It seems that controllers provide a good home for the logic of your program. – sdasdadas Dec 04 '12 at 19:34
  • Yes, you want to "separate logic" to the extent reasonably possible, but the "MVC" categorization is artificial and often harmful to good program structure. Pick the categories that are "natural" and where the links across category boundaries are minimized. When you find yourself crossing and recrossing category boundaries repeatedly to perform a simple action it's usually a good sign that you've drawn your boundaries incorrectly. The thing you'll most often find yourself needing is a layer "above" the "database" to abstract and cache it. – Hot Licks Dec 04 '12 at 20:00

2 Answers2

4

First of all usually it is a bad idea to use Observable since you need to extend this class to use it.
In any case what you are describing is an aggregation of observed entities.
Don't complicate simple things. Make each entity an observable and let it take care of registrations and notifications. You don't have to include specifically a class named Model to aggregate everything. Break your design.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • I can understand the suggestion to not use Observable. (I also found this: http://stackoverflow.com/questions/1658702/how-do-i-make-a-class-extend-observable-when-it-has-extended-another-class-too). But it seems to me that I *need* to maintain a list of Persons - there has to be some object that contains this list and it would be incorrect to hold it in a View or Controller. – sdasdadas Nov 04 '12 at 21:29
  • 2
    If you need to to be notified when a property of `Person` is updated you should look into `PropertyChangeListener` which is the norm approach in an MVC design e.g. `Swing` http://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html – Cratylus Nov 04 '12 at 21:36
  • Thank you, I was not aware of PropertyChangeListener. – sdasdadas Nov 04 '12 at 21:44
1

You can always let your subclass delegate to a contained instance of Observable. Some other ways to implement the observer pattern are mentioned here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045