    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: SINGLETON - the anti-pattern!</title>
        <link>https://members.accu.org/index.php/articles/337</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Design of applications and programs + Overload Journal #57 - Oct 2003</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c67/">Design</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c155/">57</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c67-155/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c67+155/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;SINGLETON - the anti-pattern!</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 October 2003 22:56:14 +01:00 or Thu, 02 October 2003 22:56:14 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>A pattern captures and documents good practice that has been
learned by experience. Patterns are a relative newcomer to software
development, yet have actually existed in spirit within that
community for as long as software has been developed. The point is
this: skilled software developers have always known that when
solving problems, some solutions seemed to work - with the benefit
of prior experience, some solutions just <span class=
"emphasis"><em>felt</em></span> right. A Pattern captures a problem
and a solution that works, but that's not all, for it is very rare
to find a solution that works in all circumstances. When
experienced software developers apply a solution, they do so as a
result of their experience, taking into account the context in
which the problem occurs as well as the <span class=
"emphasis"><em>tradeoffs</em></span> accepted in adopting that
solution. Therefore, a Pattern captures a problem in context,
together with a solution and its tradeoffs.</p>
<p>Patterns came to the attention of software developers in the
1990s and have accumulated a healthy body of literature. The book
<i class="citetitle">Design Patterns</i> [<a href=
"#Gamma-et-al">Gamma-et-al</a>] is the best known and the one
responsible for getting the mainstream of the community interested.
It is rather ironic and sad, that this very book is also
responsible for one of the worst red herrings ever to mislead the
software developers: Singleton!</p>
<p>According to Design Patterns the intent of SINGLETON is to:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>Ensure a class has one instance, and provide a global point of
access to it.</p>
</blockquote>
</div>
<p>Unfortunately this is rather vague, and this in itself causes
some difficulties in discussing SINGLETON, because it fails to take
into account that a SINGLETON is only meaningful if it has state.
In the absence of state, ensuring there is only ever one object of
a particular class is meaningless, because each instance is the
same as every other one.</p>
<p>I have implemented SINGLETON many times over the past several
years, and now, I can't think of one case where the SINGLETON
solution was actually a good solution to the problem it attempted
to solve. It seems to me that there are some serious problems with
the whole approach, because correspondence between problem and
solution domain models, encapsulation, and the ability to perform
initialisation, are all compromised. Further, it is now my belief
that the <i class="citetitle">Design Patterns</i> examples of where
deployment of SINGLETON is claimed to be a good approach, fail to
stand up under scrutiny (but more about that shortly).</p>
<p>I have remarked that patterns capture good ways of solving
problems. However, for every good way there are many bad ones and
some of these can be found deployed several times in practice. One
reason for the repeated deployment of a bad solution is that it
appears to solve the problem, and usually this is for one (or, for
that matter, both) of two reasons:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The problem that needs solving has not been correctly
identified, and this results in the deployed solution being a
solution to the wrong problem</p>
</li>
<li>
<p>The solution has been deployed because it really does solve the
problem, but subject to a set of tradeoffs ranging in quality from
less than optimal to downright unsuitable</p>
</li>
</ul>
</div>
<p>Such a recurring solution - i.e. one that leads to a worse
rather than better design context - is known as an <span class=
"emphasis"><em>anti-pattern</em></span>. It is my belief that
SINGLETON is not a pattern but an anti-pattern, and that its use
causes <span class="emphasis"><em>design damage</em></span>! In
this article I will attempt to state my case. I will start by
listing several reasons why I think SINGLETON is a bad idea, and
finish by making some recommendations for alternative
approaches.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e65" id="d0e65"></a>Problems</h2>
</div>
<p>According to <i class="citetitle">Design Patterns</i>, SINGLETON
is a design pattern - this means it is either language independent,
or at least applicable to several languages. I will detail what I
think the critical problems with SINGLETON are - i.e. the reasons
why I choose to use the strong term design damage - with this
factor in mind.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e73" id="d0e73"></a>Design
Models</h2>
</div>
<p>I think the best way to proceed here, is to start by going back
to the basics of interface design, and in particular the questions
of:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>How much knowledge should an object be able to assume of the
outside world in which it will be used?</p>
</li>
<li>
<p>How much responsibility should be captured within a single
interface?</p>
</li>
</ul>
</div>
<p>The answer to the first of these is simply this: an object's
knowledge of the outside world should extend to what it is told via
its interface. The purpose of an object is to provide certain
functionality to its clients, and to this end an object should
provide the minimum useful interface that makes this functionality
accessible. This underpins modularity in a design. How an object is
used in the outside world <span class=
"emphasis"><em>beyond</em></span> its interface is something that
it should - as a matter of design principle - not assume any
knowledge of. Therefore it follows that an interface can't make any
assumptions about how many objects that support that interface are
needed, because that issue is resolvable only in the outside
world.</p>
<p>The question of how much responsibility an interface should be
charged with is somewhat less concrete. An interface should be
cohesive to the point that it embodies one role in a design, but
where the boundaries of a role definition lie is by no means hard
and fast. Consider an interface supporting a simple FACTORY METHOD:
we can see that beyond whatever functionality the
interfaceprovides, its role is extended to also serve up other
objects with related roles. However, in the case of SINGLETON, the
interface must not only serve up the single object, but must also -
in addition to whatever design role it plays - promise to
managethat object. Its role therefore extends to three
responsibilities.</p>
<p>Object design affords the capability to preserve correspondence
between the problem domain model, through the stages of modelling
the solution domain, and down to the implementation code. In his
<i class="citetitle">Design</i> [<a href="#Henney">Henney</a>]
presentation Kevlin Henney uses the term <span class=
"emphasis"><em>modelarity</em></span> as meaning &quot;a measure of the
correspondence between the components of the problem being modelled
and those in its solution&quot;. This <span class=
"emphasis"><em>modelarity</em></span> factor alone plays a large
part in accounting for the effectiveness of objects in software
design. A key feature of a well designed system is the harmony
between modelarity and modularity.</p>
<p>The whole premise on which SINGLETON is based is that there can
only ever be one object of a certain class. When a design is viewed
from the perspective of preserving modelarity, it suddenly becomes
apparent that this premise is far from sound! In arguing this
particular case, I will go back to <i class="citetitle">Design
Patterns</i>, and examinewhat it claims are good uses of SINGLETON.
In the section titled &quot;Motivation&quot; <i class="citetitle">Design
Patterns</i> makes the following statements:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Although there can be many printers in a system, there should
only be one print spooler</p>
</li>
<li>
<p>A digital filter will only have one A/D converter</p>
</li>
<li>
<p>An accounting system will be dedicated to serving one
company</p>
</li>
</ul>
</div>
<p>The above three assertions all have something in common: they
describe cases of a client (the system that uses the print spooler,
the digital filter and the company) needing and using the services
of only one instance of a supplier (the spooler, A/D converter and
accounting system, respectively). Now <i class="citetitle">Design
Patterns</i> asserts that the spooler, A/D converter and accounting
system are therefore candidates for being SINGLETON. This however
is not the case and would compromise modelarity, because there is
no inherent reason why these three types of service supplier can
only have one instance - the real case is that only one instance is
needed by the client, and the real problem is that of how the
client should manage the one instance it needs. Forcing the
supplier to only ever have one instance deprives the model of its
opportunity to express the cardinality. The model suffers because
the <span class="emphasis"><em>wrong problem</em></span> has been
solved!</p>
<p>There is another way in which the use of SINGLETON compromises
the harmony between modelarity and modularity. Here the problem is
more subtle because SINGLETON appears to underpin modularity by
putting an interface and the management of its instance in one
place, but this is actually an illusion. The problem domain is the
source of and motivation for the model, but the management of
instances is a facet of the design of a software system - it does
not happen in the problem domain. Therefore, instance management is
a concern in its own right that should be separated from others. It
follows that, while it could be argued that putting an interface
and the management of its instance in one place constitutes
modularity, the argument that this is the wrong modularity is far
more compelling!</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e134" id=
"d0e134"></a>Encapsulation</h2>
</div>
<p>Encapsulation is fundamental to object oriented design. It is
the principle by which concerns are compartmentalised, and
boundaries are drawn around them. Specifically, encapsulation
manifests itself in software design in the form of implementation
detail being kept cordoned off and used only via a public
interface. It is this principle - the encapsulated implementation
being accessed via a well defined public interface - that underpins
many of the benefits that good design brings with it: clear
communication of intended usage, ease of testability etc.</p>
<p>Global variables have been known to be the enemy of
encapsulation for some time. SINGLETONs have but one instance, and
it penetrates the scopes in which it is used via a route other than
the public interface, making it the operational equivalent of a
global variable. Therefore, SINGLETONs have many of the same
drawbacks as global variables, and it is unfortunate that their
appearance in <i class="citetitle">Design Patterns</i> has lead to
so many software developers failing to notice this. In both cases -
i.e. SINGLETONs and global variables - it becomes difficult to
specify the pre and post conditions for the client object's
interface, because the workings of its implementation can be
meddled with from outside, but without going through the interface.
A consequence of the difficulties in specifying pre and post
conditions is that unit tests become harder to specify.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e144" id=
"d0e144"></a>Initialisation</h2>
</div>
<p>Usually, at the start of a program you won't have the
information needed for a SINGLETON's initialisation. Initialisation
on first use is no good, because you won't know which path will be
taken through the program until it is actually run. If there is
only one instance of a SINGLETON, then it must be initialised only
once, on or just before the first use of the (unique) object.
However, the path through the code will not be known until run
time, and so there is no way to know the point at which the
SINGLETON instance must be initialised.</p>
<p>One attempt to get around this I have seen (I'm deliberately not
using the word &quot;solution&quot;) is to attempt to initialise on every
possible control flow on which a reference to the SINGLETON is
obtained, with an exception being raised if it is used prior to
initialisation. Besides being plain ugly, this approach introduces
a maintenance headache; specifically this means:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>If a new control flow is introduced into the program and a
reference to the SINGLETON object is acquired on it, then it is
also necessary to ensure the initialisation is executed on the new
control flow.</p>
</li>
<li>
<p>When the test suite is updated to take account of the new
control flow, an additional test - i.e. a test that fails if
acquiring a reference to the Singleton object raises an exception -
will be needed to ensure initialisation has taken place</p>
</li>
</ul>
</div>
<p>All this is not to say that initialising the SINGLETON's
instance is impossible, but the number of necessary workarounds and
overheads can easily be seen mounting up.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e160" id=
"d0e160"></a>Recommendations</h2>
</div>
<p>Having given reasons why the use of SINGLETON causes damage to
software design, what recommendations can be made for alternative
approaches? It seems logical to look at the drawbacks described
above, and suggest approaches that do not suffer from the same
drawbacks. I'll start by addressing the latter two drawbacks - i.e.
initialisation difficulties and breach of encapsulation - and then
assess the situation. Consider the following two approaches, when a
<tt class="literal">Client</tt> object uses the services of a
<tt class="literal">Supplier</tt> object - <tt class=
"literal">Supplier</tt> being the object that would have been a
SINGLETON, had such a design approach been used.</p>
<div class="table"><a name="d0e174" id="d0e174"></a>
<p class="title c2">Table 1. </p>
<table border="1">
<tr>
<th>Approach</th>
<th>When it makes sense</th>
</tr>
<tr>
<td>Pass the <tt class="literal">Supplier</tt> object directly into
the <tt class="literal">Client</tt>'s methods that make use of it,
by passing the <tt class="literal">Supplier</tt> object directly
through the interfaces of those methods.</td>
<td>When the <tt class="literal">Client</tt> object is not the sole
user of the <tt class="literal">Supplier</tt> object</td>
</tr>
<tr>
<td>Pass the information (i.e. the arguments) needed to create the
<tt class="literal">Supplier</tt> object to <tt class=
"literal">Client</tt> through its interface, so that <tt class=
"literal">Client</tt> can create the <tt class=
"literal">Supplier</tt> object within its implementation.</td>
<td>When the <tt class="literal">Client</tt> object is the sole
user of the <tt class="literal">Supplier</tt> object</td>
</tr>
</table>
</div>
<p>Both these approaches are examples of the pattern PARAMETERISE
FROM ABOVE (see [<a href="#PFA">PFA</a>]). Actually, there's
something familiar about these two approaches, and so there should
be, because they're just describing normal design practices!</p>
<p>It is obvious that using either of the above approaches, there
will be no problems initialising the <tt class=
"literal">Supplier</tt> object. In both cases <tt class=
"literal">Supplier</tt> can be initialised when it is created, be
it in <tt class="literal">Client</tt> itself (latter approach) or
in <tt class="literal">Client</tt>'s client.</p>
<p>Encapsulation is also significantly strengthened, and a good way
to demonstrate this is to consider what happens when <tt class=
"literal">Client</tt>'s interface is unit tested. When the design
approach used makes <tt class="literal">Supplier</tt> a SINGLETON
the behaviour of <tt class="literal">Supplier</tt> is unpredictable
because it is outside the control of both <tt class=
"literal">Client</tt> and its client. In this scenario the
behaviour of the component under test - and hence the outcome of
the test - is affected by something invisible and uncontrollable.
Replace this scenario with one where either of the above two
approaches is used and this changes as follows:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>In the former case <tt class="literal">Supplier</tt> can be
replaced with a test implementation exhibiting behaviour designed
to test <tt class="literal">Client</tt></p>
</li>
<li>
<p>In the latter case <tt class="literal">Supplier</tt> is an
implementation detail of <tt class="literal">Client</tt>, so the
lifetime of <tt class="literal">Supplier</tt> is encapsulated
completely within <tt class="literal">Client</tt> and therefore
there is no element of randomness about it</p>
</li>
</ul>
</div>
<p>Now that initialisation and encapsulation are taken care of,
what of the issues related to models and class design? Well,
interface design seems to be in good shape: what could be more
natural than passing an object - either a supplier object, or the
information needed to create one - through an object's interface?
Unfortunately, the approaches recommended here will not
automatically avoid compromising any models. What is for certain is
that no models are automatically compromised either, which would be
the case werean approach involving SINGLETON to be used (for
reasons described earlier in this article). It was SINGLETON's
mixing of concerns in its interface - its role in the model and the
object management concern - that was problematic, but provided
participating interfaces aredesigned with attention being paid to
cohesion, this does not happen when a PARAMETERISE FROM ABOVE
approach is adopted.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e285" id="d0e285"></a>Summary</h2>
</div>
<p>For a problem and its solution to be a pattern, the solution
must be a good one in practice. SINGLETON is based on the premise
that a class must only ever have one instance, and must itself
enforce this singularity - but the premise is false because the
client of the class, not the class itself, is in a position to know
how many instances are needed. Further, breaching encapsulation and
causing initialisation difficulties cannot be good for any set of
design tradeoffs. Given the design damage that SINGLETON inflicts,
it must be considered an <span class=
"emphasis"><em>anti-pattern</em></span>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e293" id=
"d0e293"></a>Acknowledgements</h2>
</div>
<p>Thanks to Kevlin Henney and Alan Griffiths for their helpful
comments.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e298" id="d0e298"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Gamma-et-al" id=
"Gamma-et-al"></a>
<p class="bibliomixed">[Gamma-et-al] Erich Gamma, Richard Helm,
Ralph Johnson and John Vlissides, <span class="citetitle"><i class=
"citetitle">Design Patterns: Elements of Reusable Object-Oriented
Software</i></span>, Addison-Wesley, 1995.</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">Kevlin Henney, <span class=
"citetitle"><i class="citetitle"><a name="Henney" id=
"Henney"></a>Design: Concepts and Practices</i></span>, Keynote
presentation at JaCC, Oxford, 18th September 1999 (available from
www.curbralan.com).</p>
</div>
<div class="bibliomixed"><a name="PFA" id="PFA"></a>
<p class="bibliomixed">[PFA] PARAMETERISE FROM ABOVE is a term in
use, but there is currently no formal write-up.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
