Re: Singleton Pattern
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Sat, 13 Aug 2005 20:05:09 GMT
Responding to TheinvisibleGhost wrote:
I've just started working on a very large project that uses the singleton pattern to create just about everything. Argghh! Why did someone ever allow this pattern to come into existence. It just seems an incredibly posh way of using global data!
There is nothing wrong with the Singleton pattern. The problem lies in the abuses of it, such as sneaking in global data. There are very specific conditions when using Singleton is appropriate:
(1) There is only one instance of the underlying entity in the problem space and the problem solution must ensure that uniqueness AND
(2) There are multiple contexts where the entity can be instantiated within the problem solution AND/OR
(3) The instantiation context can be invoked iteratively during the execution.
IOW, one should /only/ use Singleton when the problem solution has multiple paths that could lead to violating the problem space referential integrity requirement in (1).
The most common abuse of (1) is trying to sneak in global data because it will make the solution "easier". In that case one is not abstracting the problem space in an OO fashion.
The second and third conditions are violated when Singletons are created just because there happens to be only one instance. For example, if one creates an instance only at startup in main(), there is no need for Singleton because the startup code only gets executed once so there is no possible way for the instantiation code to be invoked multiple times to create multiple instances. [One can often avoid multiple creations in (3) by simply having a boolean attribute in the object that owns the creation context. So, as a practical matter, one usually only needs Singleton when (2) applies.]
************* There is nothing wrong with me that could not be cured by a capful of Drano.
H. S. Lahman hsl@xxxxxxxxxxxxxxxxx Pathfinder Solutions -- Put MDA to Work http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman (888)OOA-PATH
.
- References:
- Singleton Pattern
- From: theinvisibleGhost
- Singleton Pattern
- Prev by Date: Re: OO and RDBMS design decision
- Next by Date: Re: State Pattern: Where to put shared behavior?
- Previous by thread: Re: Singleton Pattern
- Next by thread: Re: Singleton Pattern
- Index(es):
Relevant Pages
|