Clan x86

Technical (Development, Security, etc.) => General Programming => Tutorials, References, and Examples => Topic started by: Camel on June 24, 2008, 02:55:14 pm

Title: [Java] MVC pattern
Post by: Camel on June 24, 2008, 02:55:14 pm
Today I learned that Java has a built in interface (java.util.Observer) and (super)class (java.util.Observable) to make MVC patterns. To think all these years I've been re-inventing the wheel.

http://pclc.pace.edu/~bergin/mvc/mvcgui.html (http://pclc.pace.edu/~bergin/mvc/mvcgui.html)

Actually, TBH, I generally pretend the pattern has no merit since it's rare that you encounter a condition where multiple views/controllers is valuable, and it's a PITA to sit down and consider the implementation of the model knowing that it'll never be beneficial. Now that I'm aware of the standard construct, I might start using it more.
Title: Re: [Java] MVC pattern
Post by: iago on June 24, 2008, 04:35:46 pm
I don't think that's a MVC pattern, it's actually an, err, Observer/Observable pattern.

Maybe they're related, I'm no expert on MVC, but I don't believe that's it.
Title: Re: [Java] MVC pattern
Post by: Ender on June 24, 2008, 08:06:30 pm
I prefer not using those. I'm kind of dated on this subject, but the Observable passes a reference to itself to the Observer. This means the Observer has to access the Observable's information with a bunch of accessor methods, which is icky.

I view MVC as three layers of a program, where each layer consists of many classes; it's a way to separate/avoid dependencies.
Title: Re: [Java] MVC pattern
Post by: Camel on June 25, 2008, 03:44:04 am
MVC is an extension of the Observer pattern; (http://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/ModelViewControllerDiagram.svg/200px-ModelViewControllerDiagram.svg.png)
I'm shocked by how well that image shows up.

Ender, in order to observe an object, you have to already know what the object is (to register with its list of observers), so what's messy about gaining method-local access to it? Anonymous (in this case, Observer) types are bad; it's better to have the class that's observing extend Observer, and be able to reuse a single method to observe multiple objects, which you can't do without having the observable passed as a parameter.