Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is MVC design pattern used extensively for website development?

I am developing my knowledge of OOP design patterns and as my main focus is website development and web app development, I have tried to find examples of design patterns in these fields but seem to come across web frameworks mainly (any other examples would be appreciated). It seems to me that the majority (all?) of PHP based frameworks appear to use the MVC design pattern. As this is the most widely used would it be right to assume that it is the best design pattern for this type of development or is it a reflection of a shallower learning curve as opposed to other design patterns?

I have also noticed that the codeigniter framework uses both a singleton pattern and an MVC pattern. Is this kind of hybrid design pattern common and is it effective or is was it used in codeigniter for a specific reason?

like image 391
Matt Asbury Avatar asked Feb 07 '11 08:02

Matt Asbury


3 Answers

They use the MVC pattern by name. It's a buzzword picked because it's widely known. The way the concept is used for web applicatons has mediocre resemblence to the original pattern.

It's useful for a common nomenclature and because people have a vague understanding of what it is supposed to do. There are better patterns however, and for example Model-View-Presenter describes better what's done in practice (the term is just not used, because it's unknown). And there are other variations which have technically superseded MVC.

Singletons are another code structuring idea. It is unrelated to how you design the general application flow. It's one of a few descriptive terms intended for identifying common object structure idioms. The term is likelwise overused because of the extend of its fame. In practice it's neither important nor significant for good application design.

Here's a short overview for the design pattern names of common object APIs:
http://www.fluffycat.com/PHP-Design-Patterns/

Some discussions on the core concepts of MVC are here (maybe not relevant but highly interesting):
http://c2.com/cgi/wiki?MvcIsNotImplementable
http://c2.com/cgi/wiki?MvcIsNotObjectOriented

like image 179
mario Avatar answered Oct 26 '22 22:10

mario


With website development, there are basically two processes going on. You have the server side, where data is gathered and/or manipulated. Then there is the client side where something needs to be presented. (Some data is passed to Views in this step)

As you know from OOP, it is good practise to add structure to the data you are processing. This is where models come in. Finally, you have controllers who do processing on the models, and then pass data to the views.

You might say that MVC is the offspring of two principles: server/client side separation and OOP.

As for the singleton classes: these are indeed used for sharing resources like the database.

like image 40
Jouke Waleson Avatar answered Oct 26 '22 23:10

Jouke Waleson


Because it makes sense! :)

It's all about separating different parts of your application into specific layers. Each layer should only do things related to a specific concern. In MVC those concerns are Model, View and Controller. When you have your application divided into layers that work independently from each other you have what is called a decoupled application. This is good as it lets you test each layer without involving the other layers. In theory you can also completely replace one layer with a new implementation without changing the other layers.

  • Model - Responsible for the business logic and persistence

  • Controller - Drives the application and works as a bridge between the model and the view

  • View - Presents the model to the user

like image 32
willcodejavaforfood Avatar answered Oct 26 '22 23:10

willcodejavaforfood