Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="noreferrer">singleton pattern</a> is definitely overused and comes with its share of <a href="http://en.wikipedia.org/wiki/Singleton_pattern#Drawbacks" rel="noreferrer">difficulties</a> (unit-testing is the canonical example), but like everything in design, you need to weigh the pros and cons for your specific scenario. The singleton pattern does have its uses. There are options that may allow you to get the singleton behaviour, while alleviating some of the inherent issues:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ff647107.aspx" rel="noreferrer">Interception</a> (often referred to as aspect oriented programming, though I've seen debate that they are not exactly the same thing... can't find the article I read on this at this time) is definitely an option. You could use any combination of construction injection, the <a href="http://geekswithblogs.net/dsellers/archive/2006/09/06/90450.aspx" rel="noreferrer">decorator</a> pattern, an abstract factory and an <a href="http://martinfowler.com/articles/injection.html" rel="noreferrer">inversion of control container</a>. I'm not up on my Java IoC containers, but there are some .Net containers that allow automatic interception (I believe Spring.Net does, so likely Spring (Java) has this built in). This is very handy for any type of cross-cutting concerns, where you need to perform certain types of actions across multiple layers (security, logging etc.). Also, most IoC containers allow you to control lifetime management, so you would be able to treat your logger as a singleton, without having to actually implement the singleton pattern manually. </p> <p>To sum it up. If a singleton fits for your scenario (seems plausible from your description), go for it. Just make sure you have weighed the pros and cons. You may want to try a different approach and compare the two.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload