views:

140

answers:

4
+2  Q: 

MVC Architecture

I would like to have a clarification in MVC Architecture . While going through some of the documents, I could see that the following relationships are established between Model-View and Controller. Please help me in understanding how this is possible.

  1. View queries model’s state
  2. Model passes the state information to View
+1  A: 

Generally speaking, what you describe would be done by the controller. The controller would query the model, interpret the data, and send it to the view for display in the form the view understands.

However, MVC isn't a hard rule. There are many interpretations, and when doing it "correctly" would be uglier than bending the rules a little, the latter is usually preferable.

Andy Riordan
+1  A: 

MVC Follows some simple rules.

  • The Model speaks to the Controller
  • The Controller speaks to the View
  • The View never speaks to the Model.

Your model will generally map to a database table. For example if you have a "blogposts" table that has two columns, "title" and "body", you could do:

Blogpost.create(:title => "Hello", :body => "World")

You just created a blog post! Now, in your controller you will do something such as:

blogs = Blogpost.find(:all)

You may now pass the 'blogs' variable into your view, and it can decide how to display that data to the user. Sorry if my example code wasn't pefectly clear, it is written in Ruby (on Rails), which is my current MVC language of choice.

Mike Trpcic
In many articles I've read, they say that the View can speak to the model. It's just not the typical or canonical version of the pattern.
Dinah
A: 

Let's say you have an object called View and an object called Model. Each object holds a reference to the other. The way you described would work is that View would call a function in Model asking for the status of Model and Model would return the information it wants. A more efficient way of implementing it is that the Model will tell the State when something has changed and View will then ask for the information it wants. I don't have a code example on me, but you might want to look into using the Observer design pattern for this.

indyK1ng
A: 

I've seen a document describing the "triangular" form of MVC before, and I suppose someone at some point got it to work (either that, or documents about it are just talking about vaporware). However, I haven't found much practical use in it, as it leads to a serious software "engineering" problem: Circular dependency. If your view depends on the exact shape of the model for it to do some querying, and the model depends on the exact shape of the controller, and the controller depends on the exact shape of the view, then it becomes rather impossible to actually swap out any of those elements.

I've found it much useful to have a model that doesn't depend on anything more than a simple command line program would depend on. A view that doesn't depend on anything aside from whatever gui toolkit, or graphics drawing api you're using, and a controller that acts as a broker between the two, enabling this decoupling. This is a strategy that lets you create models and views that are modular and interchangable, leaving just a single component: the controller, with all those nasty dependencies.

Of course, there might be some kind of "Official" version of the MVC pattern, but I haven't been able to make my way through any document purporting to describe such a thing in tact, with any great sense that it really applied to real world systems. But maybe I'm just kinda dumb that way.

Breton
Yes, but if you abstract each part, the view, model, and controller to fit a mold you have in mind, then you circumvent the problem. You should always have some level of abstraction. I've managed a few working "Official" MVC programs, but you don't have to stick strictly to the pattern. You can always modify them to your needs. They're more guidelines than actual rules.
indyK1ng
I'm not sure what you mean by "abstract" in this case. Do you mean deciding ahead of time what the interfaces between the parts will be? I find that kind of approach rather difficult, I'm not really a good enough programmer to know for certain if a design is going to work ahead of time, before I've built it, and before I've had a chance to make tweaks, so depending an an interface I designed at the start, and can't change sounds scary. Maybe you mean something else.
Breton
No, I mean an abstract class or an interface which other similar objects can inherit to create a standard way of interfacing between the classes. Don't over-abstract though, that will create other issues.
indyK1ng
That's what I thought you meant. My same concern applies though. When exactly do you design that interface? It's kind of just displacing the problem, instead of a class being dependant on another class, it's dependent on the abstraction. Maybe I'm over thinking it. Maybe I'm under thinking it. Maybe I just need a nap.
Breton
That's why I generally use Java's observer. It gives you just enough abstraction where you have common classes for notification but you aren't so abstract you're defining everything you can do. As far as I'm concerned, notifications are the only things you need standardized, everything else can be direct. One thing that I learned in my Software Engineering course is that nothing can be truly independent, there will always be dependencies. The point is to keep them to a manageable level.
indyK1ng