Model - View - Controller (MVC) Architecture





What is MVC?


MVC is a design pattern. It’s not a specific piece of code as much as it is a way of organizing code into familiar reusable pieces. Design patterns are used to take your code and break it down into reusable parts that are interchangeable. So that no one part is really depending on the other too much. The concept of design patterns that computer science has, directly taken from the world to build architecture. One of the examples for the use of MVC architecture is CodeIgniter.




Why MVC?


User interfaces change often, especially on the internet where look-and-feel is a competitive issue. Also, the same information is presented in different ways. The core business logic and data is stable.
"Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice" 
- Alexander, Christopher (1977). A pattern Language: Towns, Buildings, Construction. Oxford University Press. ISBN 0195019199


How MVC work?


It split your application into three primary tasks.


1.   Controller receive user inputs
What controller doing is that he receiving the user input, then the view and the model will come to the deal. After receiving the user input you have to work with model.
2.    Processing data
Model is doing the processing any of the data, which is typically where you’re going to talk your database. If you don’t have a database then you’re going to pull data in for third party sources.
3.    Display the output
The view is primarily concerned with displaying the output you pretty much want to keep just you code in your view and you don’t want to deal any data processing in here.

Imagine a photographer with his camera in a studio. A customer asks him to take a photo of a box. The box is the model, the photographer is the controller and the camera is the view. Because the box doesn’t know about the camera or the photographer, it’s completely independent. This separation allows the photographer to walk around the box and point the camera at any angle to get the shot/view that he wants.

Non - MVC architectures tend to be tightly integrated together. If the box, the controller and the camera were one-and-the-same-object then, we would have to pull apart and then re-build both the box and the camera each time we wanted to get a new view. Also, taking the photo would always be like trying to take a selfie - and that’s not always very easy.

For your web applications you really want to use something like model view controller. Because it breaks it out into the separate parts that don’t have to rely on each other directly let’s take a sample code PHP examples.
Controller
controller.JPG
For an instance you can either insert or delete records and all the controllers doing is getting the data from the user and passing the ride along to the model. That’s the controller doing and you really want to have thin controllers. All the controllers doing is focusing on receiving the user inputs.



Model
model.JPG
Model is going to receive the data and then connect to the database in either save it, delete or modify. Anytime you want to connect to the database you really want to use a model. Because we don’t want controller to be responsible for working with the data in the database.


View
view.JPG
This is primarily concerned with the HTML. The output goes back to the browser and you don’t want to connect to the database at all here. Just want to focus on the HTML markup.

Advantages of MVC architecture


  • Separating Model from View (that is, separating data representation from presentation).
           - easy to add multiple data presentations for the same data,
 - facilitates adding new types of data presentation as technology develops.


  • Separating Controller from View (application behavior from presentation).
           - permits run-time selection of appropriate Views based on workflow,
             user preferences or Model state.        
                                            
  • Separating Controller from Model (application behavior from data representation).
           - allows configurable mapping of user actions on the Controller to
             application functions on the Model.


Disadvantages of MVC architecture


  • we need extra overhead due to layers which will do negative impact on the performance.


  • Development of user-intensive applications can sometime take longer if the layering prevents the use of user interface components that directly interact with the database.
  • The use of layers helps to control and encapsulate the complexity of large applications but adds complexity to simple applications.

Comments

Popular posts from this blog

Hash Functions

CAP THEOREM

How you can manage throttle out errors in WSO2 API Manager