![]() |
Development Lifecycle »
Design and Architecture »
Design Patterns
Intermediate
License: The Code Project Open License (CPOL)
Design Patterns - A deep dive 3By Sunny ChagantyThis article (Part 3) looks at one of the simplest and most commonly used type of Creational Design Pattern called as the Singleton Design Pattern. |
C#, Windows, WinForms, WebForms, Architect, Dev, Design
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Welcome to a series of articles on design patterns.
In the previous articles, we have covered the following areas:
Part 1 - looks at design patterns at a high level to understand what they can/can't do.
Part 2 - looks at the basic categorization of the patterns & a one-line definition of each pattern under that category.
The previous two parts have been a very high level overview of design patterns. The articles henceforth will concentrate on a single pattern & hopes to provide an insight regarding coding the pattern, uses of that pattern & some common gotcha's while using that pattern.
This article (Part 3) looks at one of the simplest and most commonly used type of Creational Design Pattern called as the Singleton Design Pattern.
What is a Singleton Pattern?
An instance of a class is considered Singleton if it meets the following criteria:
private or protected only.Why is Singleton Pattern classified as a Creational Pattern?
Creation design patterns are a category of design patterns in which there is some special process in the "creation" of an instance of a class.
In case of Singleton pattern, the constructor of the class is not accessible & so an instance of that class cannot be created. The class will internally create an instance of itself & return a handle to this instance each time.
Due to this special behavior of the constructor, this pattern is classified as a Creational Pattern.
Lets talk code
Consider a simple class calledSingleton which implements this pattern as shown:
public class Singleton{ private static Singleton m_INSTANCE = null; private Singleton(){} public static Singleton GetInstance(){ if(m_INSTANCE == null) m_INSTANCE = new Singleton(); return m_INSTANCE; } }
The interesting features of this class are: the constructor, the method GetInstance() and the private static Singleton type field m_INSTANCE, which we will get into more deeply in the following sections.
The constructor
The constructor of the Singleton class has been declared private. You could also declare it protected. Any other modifier like public or internal would allow a possibility to create an instance of this class & would defeat the purpose of a Singleton.
private Singleton(){}
The main concept to understand here is that we are restricting creating the instance of this class by setting the access modifier to the constructor. So, now no consumer of the class can create an instance of it. The only way we can get get an instance is to call the GetInstance() method & use that instance.
The field m_INSTANCE
The field m_INSTANCE is a private static field which will hold the object of the Singleton class.
private static Singleton m_INSTANCE = null;
Since this field is lazy-initialized & the GetInstance() method always returns this object, we have been able to control the creation of instances for the Singleton class.
The GetInstance() method
The GetInstance() method is the only way we would be able to get an instance of the Singleton class.
public static Singleton GetInstance(){ if(m_INSTANCE == null) m_INSTANCE = new Singleton(); return m_INSTANCE; }
In the class shown above, we note that we are lazy-initializing the private static Singleton type field m_INSTANCE. This code ensures that there would be only one instance of the type which will be created the first time GetInstance() method is called. For any other call after this, the object stored in m_INSTANCE will be returned without creating a new instance.
The point to note about the GetInstance() method is that it has been declared public static with a return type of Singleton. As mentioned previously, since the constructor is private/protected, we will not be able to create an instance of the class & so we will need this static method to get the instance.
Uses of Singleton Pattern
The Singleton pattern is extremely simple to implement & use. Typically, items which need to be initialized only once during the application life implement this pattern. Some of the common examples of such items can be: User Preferences & Settings, Thread/Connection pools, context objects, handle to Application Services (logging, data access, error handling etc.)
In short: "Singletons are most appropriate for services that donot change change their nature based on their invocation context" (c2.com, 2008)
The Bells & Whistles
In this section, we will look at some interesting items which need to be kept in mind while using the Singleton Pattern which might actually make it evil:
Wrap up
In this article we have seen how a Singleton class can be created, what are its main elements. I would love to hear your thoughts, ideas & experiences using the Singleton Pattern.
In the next article, we will look at another Creational Design Pattern.
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Jul 2008 Editor: |
Copyright 2008 by Sunny Chaganty Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |