Author Topic: [Java] MVC pattern  (Read 5834 times)

0 Members and 1 Guest are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java] MVC pattern
« 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

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.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] MVC pattern
« Reply #1 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.

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: [Java] MVC pattern
« Reply #2 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.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [Java] MVC pattern
« Reply #3 on: June 25, 2008, 03:44:04 am »
MVC is an extension of the Observer pattern;
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.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!