1

I am reading about MVC architecture. What I understand is, Modal is hold the logic part, view hold the HTML part and controller communicate between view and modal. But the use of controller is bugging me around. Why it actually needed in MVC. Why we cannot communication to Modal direct from view.

Jitender
  • 7,593
  • 30
  • 104
  • 210
  • 1
    Controller is how one interacts with the model/view e.g. controller handles the mouse, touch or keyboard input in mvc – Pramod Kumar Jul 19 '15 at 18:52
  • 1
    Think of the controller like being the boss.... telling the warehouse (model) what to pull to put on the showroom floor (view). Then processing sales (user interaction in view) and updating the ledgers (model). Somebody has to be in charge and run the store! – charlietfl Jul 19 '15 at 19:09

1 Answers1

1

It has to do with the concept of loose coupling. MVC is not the only object oriented design method but it is popular because it allows for greater object reuse.

Suppose a simple example - a website displaying information about a student's academic performance, including some analytics.

  • The view is the front-end UI displayed to the user. Could be a JSP, CSHTML, or other format.
  • The model is the data structure that makes up a student. For example, you could use a Binary Search Tree to store students, or turn it into a priority queue so that the student with the highest grade in the class/school is always at the root. At its most basic, the model represents your data and is the layer providing an interface between your database (or flat files) and your application.
  • Now, we can think of a student as a class Student. This class will contain both the behavior and attributes that comprise our representation of what a student is.
  • The controller would be class StudentController and would negotiate the interaction between class Student and the view. It is the messaging or communication layer that serves as an interface between the model and the view.

If we think of tiered architecture, it would look like this

.|.. View (top layer)
.|.. Controller (mid layer)
.|.. Model (interface b/w data and app)
▼ Database or flat files (where the data is stored)

A few resources to get you started, including similar questions:
- What is "loose coupling?" Please provide examples
- What is MVC (Model View Controller)?
- https://docs.angularjs.org/guide/controller

Boris
  • 566
  • 5
  • 21