1

I've seen multiple guys who are talking about MVC as a scalable solution for a web application. But I've got some questions about MVC. I've read on multiple sites that using static objects leads to procedural programming and not that's far away from MVC.

Do you think is this true? If so, would using singleton patterns solve this problem?

Edwin
  • 2,074
  • 1
  • 21
  • 40
D1Error
  • 15
  • 3
  • This should explain why you dont want to to use static methods: http://kore-nordmann.de/blog/0103_static_considered_harmful.html - has nothing to with MVC though. In a nutshell: Good OOP has low coupling and high cohesion. Statics lead to the exact opposite. – Gordon Aug 28 '11 at 20:11

2 Answers2

2

using static objects leads to procedural programming and that's far away from MVC

This makes no sense. MVC is a methodology to separate functionality of the application into discrete layers. Using an MVC design has nothing to do with whether or not your objects are static, or whether you use singletons. In fact, there's nothing that says an MVC design even has to be object oriented.

Most comments regarding the use of singletons deal with the difficulty in writing unit tests for them. See https://stackoverflow.com/questions/3876951/why-is-it-hard-to-unit-test-singletons for more details.

Community
  • 1
  • 1
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
2

I think Symfony2 has solved the issue quite elegantly. Instead of declaring static classes, it uses so-called "services" - there's a central manager, which contains all the services and their configurations. In different parts of your code you could get the services from the container. The container always hands out the same instance of any class required (unless specified otherwise in the configuration), which means that the singleton pattern is hardly ever needed.

The official docs make a lot better job explaining it, so here's the link about Symfony's Service Container.

I bet other frameworks also use similar methods to get rid of having the need to use singletons.

kgilden
  • 10,336
  • 3
  • 50
  • 48
  • That's something I used in my framework. Didn't knew Symfony using soemthing like this. Well, I know what to use now. – D1Error Aug 29 '11 at 06:06