Design Patterns are recurring solutions to the software design problems you find again and again in real world application development. These patterns are about design and interaction of objects and also serves as the communication platform concerning reusable solutions to commonly faced programming challenges. This post focuses on the main categories of design patterns and the implementation for the Singleton design pattern using C sharp.
Mainly design patterns are categorized in three parts
1) Creational Patterns
These patterns deals with the creation mechanism of objects, trying to create objects in a manner suitable to the situation. This type of design patterns solve the complexity of creating objects by controlling its object creations.
2) Structural Patterns
Structural Patterns are used to identify and implement the relationships between the entities in easy way.
3) Behavioral Patterns
Behavioral Patterns are self explanatory, behavior means the object methods or functions. Hence these types of patterns identifies the common ways of communications between the objects and thus increase flexibility in carrying out the communication.
Singleton Design Pattern - Creational Pattern
Singleton : Singleton design pattern is one of the Creational Patterns which controls the object creation. The main objective of the Singleton design pattern is that it creates and ensures only ONE object will be created during the life time of the application. Ex. Singleton pattern can be used for maintaining only one database connection object through out the application, maintaining the static information and the reference count of the object accessed or used by the application.
Structure
Implementation: Singleton class using C#
namespace DesignPatterns
{
public sealed class CSingleton
{
private static volatile CSingleton instance;
private static volatile int counter = 0 ;
private static object syncRoot = new object();
private CSingleton()
{
}
public static int Counter
{
get
{
return counter;
}
}
public static void Release()
{
counter--;
}
public static CSingleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new CSingleton();
}
}
counter++;
return instance;
}
}
}
}
- In above example the the class CSingleton is marked as sealed so as to restrict the derivation of this class which could odd instances of this class.
- The variable “instance” will hold the object to the CSingleton class itself, volatile keyword ensures that the assignment to the instance variable completes before instance variable can be accessed.
- Constructor is made as private to prevent creation of the object of CSingleton class by other parts of application.
- Counter data member holds count of the references accessed by the client/other part of the application. This member is used to to know how many clients are using this object.
- Above approach uses the thread safe implementation using the syncRoot data member, double - check locking is implemented to avoid the thread concurrency problems.
- Release method will just decrements the counter by one. Clients should call this method once they are done using CSingleton object. Usually when they don’t require services of this class anymore.
Popularity: 16% [?]


Leave a Reply