Click here to Skip to main content
13,501,210 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 22 Jul 2011

A Less Evil Singleton

, 22 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Wrap a singleton in a facade to make it less evil.

I’ve been working a bit more with my Griffin Framework and I’m almost ready to release the Specification library in it. The specification library is an abstraction layer similar to CommonLog and CommonServiceLocator although it contains alternatives to both of them and a bit more.

When I designed the logging interfaces I did decide to use sort of an singleton (LogManager) to be able to get a logger in each class that needs one. Since logging is such a core functionality and also well defined, I didn’t see a reason to use a regular service interface which could be used in the constructor (inversion of control). IMHO, it would only clutter all constructors since most classes will be using a logger.

The singleton facade works best for small classes that most likely will not be changed. My LogManager looks like this (comments and code contracts have been removed):

public class LogManager
        private static ILogManager _logManager = new NullLogManager();

        public static void Assign(ILogManager logManager)
            _logManager = logManager;

        public static ILogger GetLogger(Type type)
            return _logManager.GetLogger(type);

        public static ILogger GetLogger<T>()
            return _logManager.GetLogger(typeof (T));

By using this approach, all users can easily get a logger while the implementors have a lot of flexibility to generate the ILogManager which is used by the facade. The logging abstraction layer actually does not care about how the ILogManager is created or configured. It’s an implementation detail only known by the user of the actual logging framework.

I do not recommend anyone to use a singleton facade unless it’s absolutely required. Usually, it should only be used in frameworks, your own internal one or one like this.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Founder 1TCompany AB
Sweden Sweden
Skip logfiles, try automated error handling!

I'm one of the founders of codeRR, a .NET service which takes care of everything related to exceptions, so that you can focus on writing code.

blog | twitter

You may also be interested in...

Comments and Discussions

Question! can't fault this technique Pin
EbenRoux25-Jul-11 18:42
memberEbenRoux25-Jul-11 18:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 22 Jul 2011
Article Copyright 2011 by jgauffin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid