Could anyone tell me why does singleton break open/closed principle? Is it because there could be problems with inheriting from that class?
3 Answers
For a class to be "open" it must be possible to inherit from it. Inheritance is an "is-a" relationship. If you inherit from a singleton-class then instances of the child-class are also instances of the parent class due to the "is-a" relationship, meaning you can suddenly have multiple instances of the singleton class.
If the singleton class inhibits inheritance, it's no longer "open".
If a singleton class allows inheritance, and is "open" for extension, then it can no longer enforce the singleton pattern.
-
3
It is a common misconception that the Singleton design pattern prohibits inheritance. That is simply not true.
From the GoF book, page 127:
Use the Singleton pattern when
- there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
- when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
...and again on page 128:
The Singleton pattern has several benefits:
- Permits refinement of operations and representation. The Singleton class may be subclassed, and it's easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time.
Page 130 goes into detail about configuring Singleton subclasses.
The answer to why Singleton breaks the OCP is that it doesn't, at least not inherently. It just happens to be most commonly implemented in violation of the OCP by developers who have not read the GoF book. The Factory Method pattern suffers much the same fate.
-
The example in the GoF book is their
MazeFactory
class which instantiates a single instance that can vary based on an environment variable.– jaco0646Oct 7, 2020 at 15:42 -
1
-
@jaco0646, waiting to get an easy-to-understand response to what Nir said in the above comment .... Nov 22, 2020 at 18:47
-
1@IstiaqueAhmed, I recommend reading the Singleton chapter from the GoF book. A significant portion of it is dedicated to the topic of subclassing.– jaco0646Nov 23, 2020 at 15:25
There are two problems with the Singleton pattern:
- It breaks the Open/Closed Principle, because the singleton class itself is in control over the creation of its instance, while consumers will typically have a hard dependency on its concrete instance. This disallows the implementation to be changed, without having to make sweeping changes throughout the application.
- It breaks the Dependency Inversion Principle (DIP), because consumers will always depend directly on the concrete class to get the instance, while the DIP prescribes the use of abstractions. This causes the Singleton implementation to be dragged along and disallows adding cross-cutting concerns by wrapping the implementation in a decorator or distributing the client without that singleton implementation.
-
Couldn't a singleton implement an interface to solve both of these problems? The interface remains open for extension, while clients depending on the interface needn't know they are all working with a singleton instance behind the abstraction.– jaco0646Apr 27, 2016 at 13:09
-
If it had the interface and the
Instance
would be replaceable, this wouldn't be the Singleton pattern, and theInstance
property would still be defined on the static class, which causes the DIP to be violated.– StevenApr 27, 2016 at 14:00 -
-
2Hi @jaco0646: that's a great observation that a Singleton can be subclassed. Subclassing can indeed fix the OCP violation. It, however, still leaves us with the DIP violation.– StevenMar 3, 2020 at 11:28
-
1You are referring to the Singleton Lifestyle, which has little to do with the Singleton design pattern.– StevenFeb 22, 2021 at 21:30