Home arrow C# arrow Creational Patterns in C#

Creational Patterns in C#

When it comes to asking questions about creating patterns with C#, Rajesh has all the answers. Read about some C# patterns in this article.

Author Info:
By: Rajesh V S
Rating: 4 stars4 stars4 stars4 stars4 stars / 71
May 15, 2003
  1. · Creational Patterns in C#
  2. · The Factory Method Pattern
  3. · The Abstract Factory Pattern
  4. · The Builder Pattern
  5. · The Prototype Pattern
  6. · Summary

print this article

Creational Patterns in C#
(Page 1 of 6 )

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#
/*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;
   return null;
 protected void Finalize()
  instanceFlag = false;
class MyClient
 public static void Main()
  SingleInstanceClass sic1,sic2;
  sic1 = SingleInstanceClass.Create();
  if(sic1 != null)
  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.

blog comments powered by Disqus

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials