<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">thalesc</title><subtitle type="html" /><id>http://blogs.msdn.com/b/thalesc/atom.aspx</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/thalesc/" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/b/thalesc/atom.aspx" /><generator uri="http://telligent.com" version="5.6.50428.7875">Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><updated>2011-02-24T16:32:00Z</updated><entry><title>Favor Composition Over Inheritance</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/thalesc/archive/2012/09/05/favor-composition-over-inheritance.aspx" /><id>http://blogs.msdn.com/b/thalesc/archive/2012/09/05/favor-composition-over-inheritance.aspx</id><published>2012-09-05T23:00:00Z</published><updated>2012-09-05T23:00:00Z</updated><content type="html">&lt;p&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span style="font-size: small;" size="3"&gt;Making code reusable through public class inheritance (PCI) is so convenient and easy that to say it should be avoided may sound a bit heretical. After all, isn&amp;rsquo;t this what OOP is about? And yet that&amp;rsquo;s the position I hold.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span style="font-size: small;" size="3"&gt;To be clear, by PCI I refer to inheriting from an implementation class, not from an abstract interface.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;The reason I don&amp;rsquo;t regard PCI as a premium reuse mechanism has to do with all the extra coupling. Class inheritance (&amp;ldquo;is-a&amp;rdquo; relationship) is a stronger form of coupling than composition (&amp;ldquo;uses-a&amp;rdquo; relationship). A class that offers its services primarily through PCI is basically saying &lt;em&gt;&amp;ldquo;to use my services you can&amp;rsquo;t just hire me, you must become me&amp;rdquo;&lt;/em&gt;. That&amp;rsquo;s a strong commitment!&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;The &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle" target="_blank"&gt;&lt;span style="color: #0000ff;" color="#0000ff"&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Liskov Substitution Principle&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;span style="font-size: small;" size="3"&gt; (LSP) says that a derived class must not break the contract of its base classes. In other words, it must be possible to pass an instance of a derived class to code that only knows about the base class without breaking that code&amp;rsquo;s correctness. The classic counter-example is to derive Square from Rectangle and then pass an instance of Square to code that modifies the width of a Rectangle. However the PCI is implemented, virtual or not, it&amp;rsquo;d be incorrect. Either a square will end rectangular (thus breaking its type&amp;rsquo;s traits) or the code expecting Rectangle will observe it behave rather non-rectangularly. Therefore per LSP one should not define Square in terms of a PCI of Rectangle, period.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;But LSP is not just about what makes good derivations. It&amp;rsquo;s also about what makes good bases. A corollary of LSP is that a class intended for reuse through PCI must not put its derivations in a position of breaking LSP. Note this implies the base class is committed &amp;nbsp;not only to its own interface, but weirdly to the interfaces of all its derivations too. That&amp;rsquo;s the funny thing about PCI, it increases the dependency both ways. In the Square/Rectangle example, if we were to disregard &lt;span style="text-decoration: line-through;"&gt;PCI&lt;/span&gt; LSP and force the inheritance while still trying to maintain some sort of semantic consistency, that&amp;rsquo;d very likely impact Rectangle&amp;rsquo;s interface. Not only that, now any future change to Rectangle&amp;rsquo;s interface must consider the impact on Square, because now Rectangle&amp;rsquo;s interface belongs to Square too! The flip-side of &amp;ldquo;&lt;em&gt;you must become me&lt;/em&gt;&amp;rdquo; is &amp;ldquo;&lt;em&gt;well, in part that makes me become a little like you too&lt;/em&gt;&amp;rdquo;. A base class is forever responsible for the derived classes it has tamed.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;Another thing to watch for is that a base class is too convenient a place to put code that is common among its derivations, and very often that convenience gets in the way of good design. The shared code starts to bubble up to the common ancestors (it&amp;rsquo;s just too convenient) whether it has to do with the class&amp;rsquo; original responsibilities or not. Sometimes that code implements a policy that would be better as a late decision, but now the class is forcing its descendents to commit to that policy upfront. I&amp;rsquo;m sure you have seen it. You need to derive from A to reuse some logic, but A&amp;rsquo;s grand-grandparent imposes unrelated behavior you&amp;rsquo;d rather not have in your class. It&amp;rsquo;s the family curse! Big ripple in the code. In PCI-heavy code, classes have a propensity to become hoarders of functionality, becoming more bloated, complex and brittle as inheritance tree goes down. With Composition, classes tend to stay smaller and more focused (i.e., &amp;lsquo;simpler&amp;rsquo;), and then you can pick-and-choose the functionalities you need.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;That&amp;rsquo;s not to say PCI doesn&amp;rsquo;t have its uses. Sometimes you do have a genuine &amp;ldquo;is-a&amp;rdquo; relationship. Other times the convenience of PCI is worth the trade-offs and you can keep the usage limited and the hierarchy flat (but you should be ready to refactor at the first sign of trouble). The important thing here is to understand the implications in order to make good design decisions. PCI is a valid OOD technique, but it&amp;rsquo;s not a panacea. Here&amp;rsquo;s a quick comparison of PCI and Composition.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;table style="border: 1px solid #000000;" border="1" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p align="center"&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Inheritance (is-a)&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p align="center"&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Composition (uses-a)&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;&lt;/span&gt;&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-43-72/5850.Inheritance.gif"&gt;&lt;img style="margin-right: auto; margin-left: auto; display: block;" border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/203x86/__key/communityserver-blogs-components-weblogfiles/00-00-01-43-72/5850.Inheritance.gif" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-43-72/4034.Composition.gif"&gt;&lt;img style="margin-right: auto; margin-left: auto; display: block;" border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/204x105/__key/communityserver-blogs-components-weblogfiles/00-00-01-43-72/4034.Composition.gif" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;The sub-class and super-class interfaces are tightly coupled.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;The front-end and back-end interfaces are loosely coupled.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Changes to the interface of the super-class also change the interface of the sub-class and are more likely to ripple to the sub-class&amp;rsquo; consumers.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Changes to the interface of the back-end may change the implementation of the front-end, but are less likely to ripple to the front-end&amp;rsquo;s interface and consumers.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Every change to the interface of the sub-class must be compatible with the interface of the super-class.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Not every change to the interface of the front-end needs to be compatible with the interface of the back-end.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;The sub-class must pick the super-class upfront, at compilation time.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;The front-end can delay selecting the back-end until needed, at runtime.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;The super-class must be instantiated with the sub-class and remains for the lifetime of the sub-class.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;The back-end can be instantiated only when needed and destroyed earlier than the front-end.&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;It&amp;rsquo;s easy to add new sub-classes because inheritance comes with polymorphism&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;It&amp;rsquo;s hard to add new front-ends because delegation has to be written by hand, even when it&amp;rsquo;s a just bypass&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="310"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Calls to the interface of the super-class can be dispatched directly to the sub-class through polymorphism &lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="309"&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;Calls to the interface of the front-end must be relayed to the back-end programmatically&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt;References:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Composition_over_inheritance"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;Composition Over Inheritance (Wikipedia)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.artima.com/lejava/articles/designprinciples4.html"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;Composition versus Inheritance (A Conversation with Erich Gamma)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://books.google.com/books?id=4pjbgVHzomsC&amp;amp;pg=PA17&amp;amp;lpg=PA17&amp;amp;dq=composite+reuse+principle&amp;amp;source=bl&amp;amp;ots=snkbw6h5Te&amp;amp;sig=S3UJrrDH_hU5V0AcWJQqLsE3cSY&amp;amp;hl=en&amp;amp;ei=ppKIS4arIIz4M-WE8KYB&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=10&amp;amp;ved=0CDAQ6AEwCQ#v=onepage&amp;amp;q=composite"&gt;&lt;span style="color: #0000ff; font-family: Calibri; font-size: small;" face="Calibri" size="3" color="#0000ff"&gt;Composite Reuse Principle (CRP)&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;" size="3"&gt;&lt;span style="font-family: Calibri;" face="Calibri"&gt; (from Java Design: Objects, Uml, and Proccess; Google Books)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="font-family: Calibri; font-size: small;" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346745" width="1" height="1"&gt;</content><author><name>Thales C</name><uri>http://blogs.msdn.com/thalesc_4000_hotmail.com/ProfileUrlRedirect.ashx</uri></author><category term="design" scheme="http://blogs.msdn.com/b/thalesc/archive/tags/design/" /></entry><entry><title>Singletons are Evil</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/thalesc/archive/2011/02/24/singletons-are-evil.aspx" /><id>http://blogs.msdn.com/b/thalesc/archive/2011/02/24/singletons-are-evil.aspx</id><published>2011-02-25T00:32:00Z</published><updated>2011-02-25T00:32:00Z</updated><content type="html">&lt;p&gt;&lt;span style="color: black; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="color: black; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"&gt;
&lt;p&gt;&lt;span style="font-family: verdana,geneva;"&gt;Singletons are evil. I hate them. No, "hate" is a strong word. I dislike them. They're inherently easy to misuse. I guess the problem gets aggravated by the fact that the &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Singleton_pattern"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Singleton&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt; is the simplest of the &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Design Patterns&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt; to adopt and implement, so it tends to pop everywhere. I know because I've been there. My first project after learning about patterns was peppered with singletons. When you get a new hammer everything starts to look a lot like a nail. Today I&amp;nbsp;avoid them&amp;nbsp;as much as possible. I guess you should too. Not that you can't use them, but you should know the kind of trouble they can cause.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Software Design is a lot about the &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;management of dependencies&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;. If you're not thinking about dependencies when you're doing design, I'm sorry, but whatever you think you're doing, however fancy you think it is, it's not design.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;When designing a piece of software you try to eliminate unnecessary dependencies as much as practically possible, to make the piece have fewer reasons to change. This is not to prevent change, on the contrary, by given fewer reasons for something to change you make change easier elsewhere. The idea is to control how change can propagate through the hierarchy, so changes can happen without triggering a devastating ripple of more and more changes.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Dependencies come not only from other software components, but also from polices, user requirements, decisions, etc. And hence the problem with singletons, they commit to a decision about their cardinality way too early in the design.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Here's an example.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: courier new,courier;"&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; class CConfigManager&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; static&amp;nbsp;CConfigManager* getInstance();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Classic. I see this sort of thing a lot. Now, you may have been thinking back there that a single instance of the configuration manager is all your application is ever going to need, so you may as well bake the trait directly into the class itself, right? That's very convenient too, because now any code that needs the instance can go grab it directly, which means fewer references to pass around. Besides, isn't it better to have code that is smart and doesn't need to be told what to do all the time? There's a catch though: &lt;em&gt;in most cases the decision that there can be only one of something is an application level responsibility&lt;/em&gt;. Ultimately it should be up to the application to decide it'll need only one of whatever, ever. Yet you pulled that high level decision all the way down to a lower level layer and cemented it there, and that changed things around. Soon pieces of code everywhere start to take a hard dependency on the fact that CConfigManager is a singleton. The code ought to know that because it ought to call getInstance. This increases the coupling between the code and the singleton class unnecessarily. If in one hand the code doesn't need to be told what to use, on the other hand it can't be told to use anything else.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Then one day a requirement for a new feature comes in. The feature is to consolidate two reports of the thing your thing generates a report of, but in order to do that the new code will have to instantiate two sets of objects, each set pointing to a different configuration manager. Basically the application decided it needs two instances of the object now. It was&amp;nbsp;the application's prerogative&amp;nbsp;all along anyways.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Congratulations, you just got yourself some extensive refactoring to do. And good luck doing that without unit-tests, which you obviously don't have, because had you been using unit-testing you'd have realized singletons make the "units" a lot harder to test, in which case you'd have got rid of it earlier on.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;More good stuff on the&amp;nbsp;badness of singletons:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Why Singletons are Evil&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/webservices/library/co-single.html"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: arial,helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: verdana,geneva;"&gt;Use Your Singletons Wisely&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10133826" width="1" height="1"&gt;</content><author><name>Thales C</name><uri>http://blogs.msdn.com/thalesc_4000_hotmail.com/ProfileUrlRedirect.ashx</uri></author></entry></feed>