The most used and (in)famous design pattern in the world is causing us great harm.
TL;DR: Don't use Singletons. Ever.
-
Coupling
-
Testability
-
Accidental problems
-
Multi threading
-
Premature Optimization
Code Smell 20 - Premature Optimization
-
Avoid it.
-
Use contextual unique objects.
-
Benchmark object creation.
-
Database Access
-
Globals
-
Logging
<?
class God {
private static $instance = null;
private function __construct() { }
public static function getInstance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
}
<?
interface Religion {
// Define common behavior for religions
}
final class God {
// Different religions have different beliefs
}
final class PolythiesticReligion implements Religion {
private $gods;
public function __construct(Collection $gods) {
$this->gods = $gods;
}
}
final class MonotheisticReligion implements Religion {
private $godInstance;
public function __construct(God $onlyGod) {
$this->godInstance = $onlyGod;
}
}
// According to Christianity and some other religions,
// there’s only one God.
// This does not hold for other religions.
$christianGod = new God();
$christianReligion = new MonotheisticReligion($christianGod);
// Under this context God is unique.
// You cannot create or change a new one.
// This is a scoped global.
$jupiter = new God();
$saturn = new God();
$mythogicalReligion = new PolythiesticReligion([$jupiter, $saturn]);
// Gods are unique (or not) according to context
// You can create test religions with or without unicity
// This is less coupled since you break the direct reference to God class
// God class Single Responsibility is to create gods. Not to manage them
This is a design pattern. We should avoid it by policy.
We can add linter rules for patterns like 'getInstance()' so new developers cannot infect code with this anti-pattern.
- Globals
This is an historical mistake already acknowledged by the community. Nevertheless, lazy developers bring it up again and again. We need to reach a consensus on its drawbacks.
Code Smell 06 - Too Clever Programmer
Code Smell 25 - Pattern Abusers
Singleton - The root of all evil
Photo by Maria Teneva on Unsplash
The Diagram is Not the Model. The model is not the diagram. It is an abstraction, a set of concepts and relationships between them.
Eric Evans
Software Engineering Great Quotes
This article is part of the CodeSmell Series.