Why is MVC design pattern used extensively for website development?
I am develo开发者_StackOverflow中文版ping 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?
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
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.
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
I think you are confusing some concepts here. First of all, MVC is not exactly a design pattern but more a general concept of organizing an application. B/c there is not one or the best implementation for an MVC. MVC is just what your common sense tells you about structuring data processing. Seperating what you see, from what is going on internally and what information is processed at all.
Singletons are usually used b/c in an MVC you have lots of different objects using the same resources. To organize that you can use Registry implemented as a singelton to give access to the DB for example.
Best regards
Raffael
EDIT:
actually there are many different views on what a useful realization of an MVC-structure should look like.
For example, in books you usually see three boxes, labelles 'model', 'view' and 'controller'. And they are all connected by arrows. IMO the connection between 'view' and 'model' should be left out, cause the model should be handled by the controller which communicates with the view.
Also I think it is very important to distinguish between business logic and the controller. Get's easily mixed up. But BL belongs to the model tier. In that sense I don't think of the MVC as a something like a triangle but more of a three-tier-system V/C/M.
In general:
- MVC (Model–View–Controller) is a software architecture.
Important: there is no MVC design pattern ;) - A design pattern is a general reusable solution to a commonly occurring problem in software design. In other words: it as the best practice for a common problem.
- Examples: Singleton, Factory Method, Adapter, Decorator, etc.
So why is MVC used that often?
The reason is quite simple: If all developers of a project are familiar with MVC (or another architecture) they have a common understanding on where to place code or where to look for code. It's about being effective and write good code. So at the end it is about having a common understanding.
精彩评论