Would Singleton be a good design pattern for a microblogging site?

The Singleton's purpose is to limit object instances to one and to provide global access Both are things you don't want or need.

The Singleton's purpose is to limit object instances to one and to provide global access. Both are things you don't want or need. Limiting your instance to one instance is rather pointless in PHP where this restriction only applies to the instances in the current request.

If two requests hit your microblogging site at the same time, there will still be one instance each per request. If you want to make sure there is only instance, simply do not instantiate a second instance. Global access is nothing you want either, because it breaks encapsulation.

If you need a certain instance inside your objects, pass it in via Dependency Injection. That's clean and maintainable. It has the added benefit of allowing you to easily exchange dependencies with other implementations, like for instance Mock classes for your UnitTests.

Even Erich Gamma, one of the Singleton pattern's inventors, doubts this pattern nowadays: "I'm in favor of dropping Singleton. Its use is almost always a design smell" You are best off avoiding Singletons.

If I am not mistaken Singleton is antipattern. But depending on the task it can be used.

1 for AntiPattern – Gordon Oct 6 '10 at 8:26 That's pretty controversial. It's overused, and comes with significant caveats, but it fulfils a distinct function and I have a hard time saying it's always wrong. – annakata Oct 6 '10 at 8:27 It's not always wrong.

When having more than one of something will cause things to blow up, Singleton is a good way to prevent disaster. But it's wrong in most cases, and it's too often misused/abused. It should never have been added to a list of common solutions to common problems (which, when you break it down, is what patterns were meant to be).

Most people should almost never be using Singleton, and probably wouldn't if it weren't blessed by the GoF. – cHao Oct 6 '10 at 8:55.

As said before, the singleton doesn't help you with your blog application much. Just create a single database instance and use it. Sidenode, what you see in PHP are often "fake singletons".

If implemented as plain class, it usually involves using a ::getInstance() method which implements the singleton workaround. The existence of the class however allows to instantiate multiple items (new Singleton() && new Singleton()). Therefore I'd recommend a procedural singleton, which doesn't have this problem and is also much nicer on the eyes: function db() { static $db; if (!isset($db)) { $db = new PDO("sqlite:memory"); } return $db; } This way you can use db()->query("SELECT * FROM blog") and avoid to always import a global $db var.

Functions are defined in the global scope, so using this within a class has the same coupling and encapsulation problem as global $db; or DB::getInstance(); – Gordon Oct 6 '10 at 9:50 Also, since db() returns a PDO object, I can simply clone it to get multiple instances. Try var_dump(db(), clone(db())); – Gordon Oct 6 '10 at 10:03 Same as all objects in PHP. The OOP semantics don't allow for real singletons.

– mario Oct 6 '10 at 10:56 Not true. You can do private function __clone() {} and private function __construct() to prevent multiple instantiation and cloning. See the Singleton example in the PHP Manual – Gordon Oct 6 '10 at 11:29 Prevention ok.

Though it would be a more proper singleton if __clone could just return $this. Or if there was a way to have new Singleton() == new Singleton() without helper methods and workarounds. – mario Oct 6 '10 at 11:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions