6

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?

Matt Asbury
  • 5,644
  • 2
  • 21
  • 29
  • 2
    It is not pattern what widely used but just a name. – Your Common Sense Feb 07 '11 at 09:13
  • possible duplicate of [Why is the MVC paradigm best suited for web applications?](http://stackoverflow.com/questions/2347710/why-is-the-mvc-paradigm-best-suited-for-web-applications) – Gordon Feb 07 '11 at 09:29
  • @Gordon - I think the question you mentioned focuses on why use an MVC pattern at all where I am trying to understand why it seems to be most prolific through a specific field and whether there are better alternatives. I would see why you would suggest it from the title though. Please feel free to edit it if you believe it can be phrased more accurately. – Matt Asbury Feb 07 '11 at 09:34
  • 1
    possible duplicate of [Who needs Singletons](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323) – Gordon Feb 07 '11 at 09:36
  • 1
    @Matt it is the most prolific because some years ago every web framework tried to be Ruby on Rails :) – Gordon Feb 07 '11 at 09:41

5 Answers5

3

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

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • Well your description of Singleton seems somewhat complicated to me :-/... a singleton is simply a pattern that assures that only one instance of a class can be created. – Raffael Feb 07 '11 at 09:28
  • 1
    @Raffael that should read "a [singleton is an anti-pattern](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323) that assures a) only one instance of a class can be created and b) that instance can be globally accessed." – Gordon Feb 07 '11 at 09:33
  • A singleton is not necessarily an anti-pattern. it depends on the context. – Raffael Feb 07 '11 at 09:35
  • @Raffael: True. It very much has its uses. But it's also overused. We see a few "How can I design social network with singleton?" questions per day, where the term has obviously no resemblence to the domain problem but was just picked due to popularity. (That's why I tried to nonchalantly badmouth it above instead of explaining.) – mario Feb 07 '11 at 09:40
  • @Raffael care to give a context where it is not an AntiPattern? /cc @mario – Gordon Feb 07 '11 at 09:48
  • @Gordon: Me supposed to come up with a senseful incarnation too? Hmmm, not easy to think one up. But I use something that resembles a procedural singleton (upping the ante of weirdness). Basically `db()->prepare("SELECT *);` though it's less about single instance, that's just a by-product. Well and for config registries it seems okay (though unneeded). – mario Feb 07 '11 at 09:54
  • @mario thanks. I dont get the procedural singleton example. Guess it's too brief. As for the config registry: you already said it yourself. It's YAGNI :) – Gordon Feb 07 '11 at 09:58
1

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.

Raffael
  • 19,547
  • 15
  • 82
  • 160
  • 1
    I've come across these diagrams you mentioned and I agree they don't seem to make sense. The idea of having the separation between model and view and using the controller to handle the flow between them and then having a diagram in the next paragraph demostrating a link between the model and the view seems foolish. – Matt Asbury Feb 07 '11 at 09:30
1

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.

Jouke Waleson
  • 517
  • 1
  • 3
  • 14
1

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

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
1

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.

udo
  • 4,832
  • 4
  • 54
  • 82