Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: why do we need "controller", or when should we use this pattern?

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?

like image 895
Dmitry Frank Avatar asked Dec 02 '12 17:12

Dmitry Frank


People also ask

Why should we use MVC pattern?

The MVC pattern helps you break up the frontend and backend code into separate components. This way, it's much easier to manage and make changes to either side without them interfering with each other.

Why are controllers needed?

Controllers improve the steady-state accuracy by decreasing the steady state error. As the steady-state accuracy improves, the stability also improves. Controllers also help in reducing the unwanted offsets produced by the system. Controllers can control the maximum overshoot of the system.

When would you use a Model View Controller?

Basically, MVC serves well when you have an application that needs separation of the data(model), the data crunching(controller), and the presentation of the data(view). This also serves well in an application where the data source and/or data presentation can change at any time.

How does MVC controller works?

How MVC Architecture works. First, the browser sends a request to the Controller. Then, the Controller interacts with the Model to send and receive data. The Controller then interacts with the View to render the data.


2 Answers

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.

like image 61
Felipe Alarcon Avatar answered Oct 13 '22 20:10

Felipe Alarcon


"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.

like image 32
nooitaf Avatar answered Oct 13 '22 20:10

nooitaf