14

I have read many publications about MVC, but I still can't clearly understand why do we need "controller".

I usually write applications in client-server model:

server contains all the business-logic, and it knows nothing about the gui. It does the main job, and it is as portable as possible.

client is a GUI, it binds to the server, interacts with user, sends commands from user to the server.

I like this architecture, and I can't figure out why do people really need one more medium between client and server, which seem to be controller?

UPD: simple example: assume we need to write some data logger. Data comes from the COM port, it is encoded by some protocol. Need to show received messages in a simple log window.

How would I make it:

server contains the following items:

  • Data_receiver: actually receives raw data from the COM port, but it's interface, so we are able to make some another class that receives data from any other source;
  • Data_decoder: takes raw data and returns resulting decoded messages, it's interface too, so we can change encoding protocol easily;
  • Data_core: using instances of Data_receiver and Data_decoder, emits signals to clients.

client contains the following items:

  • Appl core: creates instance of Data_receiver (the one that connects to COM port), Data_decoder and Data_core (which takes references to Data_receiver and Data_decoder instances), also creates GUI simple log window (which takes reference to Data_core);
  • GUI simple log window: binds to the Data_core, i.e. listens for the signals emitted by it, and displays received data.

As I understood what I have read about MVC, GUI should not actually take received messages from the Data_core, because controller should do that and then pass data to the GUI. But what bad things happens if GUI takes this data directly from the model?

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • 3
    I like this question, simply because no-one has given a "you're doing it wrong" answer. That means the community at large either does not know the answer to this question (unlikely), or that MVC and MVVM are overplayed to the point where individuals like yourself think that they are *required*, when, in fact, sometimes they can get in the way, and take away from code readability. – cwharris Dec 02 '12 at 20:35
  • 2
    Additionally, patterns like MVVM and MVC are *patterns*. That means that they *appear* in your code when you're writing SOLID code. In other-words, if you're writing SOLID code, and the patterns are not appearing, you're likely writing code which does not need these patterns. – cwharris Dec 02 '12 at 20:37
  • Furthermore, in your example, both your client and your server have controller code. Example: a text field is only a text field. Validation logic that is applied to that text field is *controller* code. If you don't find yourself repeating that validation logic, you're probably writing the code in a DRY manner, which does not need a bona fide "controller" class. On the other hand, if you've got two views both doing the same thing to their text fields (converting them to integers, making sure they're between 1-100), you might want to consider subclassing textfield and adding controller logic. – cwharris Dec 02 '12 at 20:40
  • Note: I'm writing these as comments because I'm sure there are many people who will down-vote this kind of thinking. Therefore, you should take what I say with a grain of salt, and consider the fact that there are many many developers who believe that bona fide MVVM implementations are the way of the future. – cwharris Dec 02 '12 at 20:44
  • MVC has nothing to do with whether your product is client-server or not. – Neil McGuigan Dec 03 '12 at 09:25
  • @ChristopherHarris, thanks for your comments. I'll look into MVVM (hate to say, never heard about it before) – Dmitry Frank Dec 03 '12 at 12:23
  • @DmitryFrank, in case you're unfamiliar with SOLID design, definitely check it out. You'll get a better understanding of how to analyze scenarios like this, and apply patterns that fit. http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) – cwharris Dec 03 '12 at 20:44

4 Answers4

2

In the past I have asked myself this same question many times and I have recently been reading about JSP model 2 architecture, and the wikipedia entry states the following.

The literature on Web-tier technology in the J2EE platform frequently uses the terms "Model 1" and "Model 2" without explanation. This terminology stems from early drafts of the JSP specification, which described two basic usage patterns for JSP pages. While the terms have disappeared from the specification document, they remain in common use. Model 1 and Model 2 simply refer to the absence or presence (respectively) of a controller servlet that dispatches requests from the client tier and selects views.

That basically means that there are variations to the MVC pattern itself so you can always apply a MVC or MV pattern depending on your project. However a proper MVC architecture should indeed have a controller as the model and view should not talk to each other directly.

Felipe Alarcon
  • 948
  • 11
  • 19
1

"client-server" has nothing to do with MVC, afaik.

I understand it like this:

  • The Model is the way you structure your data.
  • The View is the visible representation. (GUI)
  • The Controller uses the logic to control the view and/or other logic.

The idea behind it is to split the visual representation from the logic. So when you grab the View you dont duplicate the logic. ... so in your case you might use MVC only on the client side and you need a controller, because thats where all the magic happens.

nooitaf
  • 1,466
  • 11
  • 18
  • But logic and visual representation are splitted already by client-server architecture: server has the data and logic, client represents data. I updated my question (added simple application example), please comment it somehow (what in it should be model, what should be view, and what should be controller) – Dmitry Frank Dec 02 '12 at 18:47
  • I would guess the `Data_core` is your controller. The `window` you create is the view. The MVC architecture kicks in when u have multiple windows with different things happening because your Data_core would get quite complicated. So you would try to create little islands of controllers which just do the logic of their corresponding view. Then you would connect all controllers to the Data_core. – nooitaf Dec 02 '12 at 19:15
1

I think of the MVVM pattern in reading the details of your client-server pattern. When you wish to perform unit tests it is better to look at the VM (ViewModel) as the controller.

Following your pattern, a classical client/server pattern, the Controller,Model, and View live in the server code that you call Data_receiver, Data_decoder, and Data_core. In your 'client' you again have a controller, and view.

It is useful to separate from the server code a Controller that receives and decodes the signals and data from the COM, a Model to which the Controller passes and receives data in route to and from the database, a View that encodes and formats the raw data from the Model.

In following the Don't Repeat Yourself (DRY) principle your may notice that there is controller code in both the server and client portions of your code where you repeat code or responsibilities.

It is useful when you are driving your development in Test Driven Development fashion to separate into distinct parts, such that you may attach various tests.

mozillanerd
  • 568
  • 4
  • 9
-1

Better late than never I want to correct your misinterpretation of "Why one more layer between client and server is needed."

IF you go through the following lines the answer is crystal clear that MVC is triangular model of architecture.

View asks request to Model, Model asks Controller, Controller processes it and sends back to View.

The Model is the way you structure your data.
The View is the visible representation. (GUI)
The Controller uses the logic to control the view and/or other logic.

Regards, Pavan.G

Pavan Kumar
  • 190
  • 2
  • 8
  • Thanks for the answer, but I already have read about "triangular model" and so on. There's realy unclear for me, WHY is it so good that so many people talk about it. I'd appreciate if you would write MVC-way to design simple logging application that I mentioned in my question. – Dmitry Frank Dec 03 '12 at 12:20