Creational Patterns in C#
(Page 1 of 6 )
When it comes to asking questions about creating patterns with C#, Rajesh has all the answers. Read about some C# patterns in this article.
The software design patterns are mainly classified into three categories, namely Creational Patterns, Structural Patterns and Behavioral Patterns. The Creational Patterns deals with the best way to create objects. The Singleton Pattern is an example of Creational Pattern.
The singleton design pattern is used when only one instance of an object is needed throughout the lifetime of an application. The singleton class is instantiated at the time of first access and the same instance is used thereafter till the application quits.
The famous GOF defined the Singleton Pattern as follows.
“Ensure a class has only one instance, and provide a global point of access to it.” -- "Design Patterns” Gamma et al., Addison-Wesley, ISBN:0-201-63361-2”
The Singleton class can be used in various places where one would need a common repository of information that can be accessed from all objects in an application. For example sometimes we may need a single Database connection object or Network connection object.
Non-software Example
The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54]
It is pretty easy to implement the Singleton Pattern in C#. There are lots trivial ways to achieve this. But by using a private constructor and a static method to create an instance of the class is a popular way to create singleton pattern.
The above program will display OK and then followed by NO MORE OBJECTS. The value of the object sic2 is null, because we can’t create two or more instances of the class SingleInstanceClass.
C# Implementation
//Creational Pattern: SINGLETON
//Implemenation in C#
//By rajeshvs@msn.com
/*The constructor should be private. Provide a static method, which returns an instance of the class. use a static variable to check whether already one instance is created or not. if already an instance is there , returns a null */
using System;
class SingleInstanceClass
{
private static SingleInstanceClass sic= null;
private static bool instanceFlag = false;
private SingleInstanceClass()
{
}
public static SingleInstanceClass Create()
{
if(! instanceFlag)
{
sic = new SingleInstanceClass();
instanceFlag = true;
return sic;
}
else
{
return null;
}
}
protected void Finalize()
{
instanceFlag = false;
}
}
class MyClient
{
public static void Main()
{
SingleInstanceClass sic1,sic2;
sic1 = SingleInstanceClass.Create();
if(sic1 != null)
Console.WriteLine("OK");
sic2 = SingleInstanceClass.Create();
if(sic2 == null)
Console.WriteLine("NO MORE OBJECTS");
}
}
The above program returns a null value when try to create an object second time. But instead of returning null, it is possible to return already existing object ‘sic’ by changing ‘return null’ to ‘return sic’ in the above program.
Next: The Factory Method Pattern >>
More C# Articles
More By Rajesh V S