Singleton Pattern

Abstract

One of the types of the design patterns is the creational pattern which is connected with the object instance construction. Such pattern separate the creation of the objects within the application from its operation, and as such; decoupling of object creation from the application operation which gives the application the flexibility to configure all aspect of object creation. Such configuration can be static (at compile-time) or dynamic (at run-time). One of the creational pattern is the singleton pattern where it can be used to ensure that only one instance of an object is created (Bennett and Farmer, 2002).

Singleton Pattern

Bennett and Farmer (2002) explained that the following advantages and disadvantages of the Singleton Pattern:

  • Singleton pattern provides the implementation of a controlled access to the sole object instance.
  • Singleton pattern can be used to create a specified number of instances if such behavior is required within the application.
  • Using the Singleton pattern presents some additional message passing where an operation has to be accessed first instead of accessing the instance directly.
  • The singleton pattern limits the flexibility of the application, since any requirement if many instances of the singleton object is required to be implemented in the application to accommodate such request, will require a significant modifications to the existing system.

Study Case – Implementing Singleton Pattern

The Airline employee management system required to hold information regarding the company information such as company name, the company address to be stored so that it can be displayed in all application GUIs interface and also all the system reports. Such information should be held in one place within the application where such information can be accessed and used by many different objects within the system. One design approach can be implemented by creating global data variables that will have a public scope within the application; however such approach will violates the principle of the encapsulation. Also, any changes to the structure of the global data will impact the structure of all objects that implement the access to the global data structure (Bennett and Farmer, 2002).

Bennett and Farmer (2002) suggested another approach that is to create the company class where it can overcome the encapsulation problem by encapsulating the company attributes within Company class:

Company Class

-companyName-comapnyAddress -companyRegistrationNumeber+getCompanyDetails
Such solution implements the static operation (getCompanyDetails() where it provides global access without the need of instantiating the class. Calling the static operation will be as follows:

Company.getCompanyInstance()

However, to ensure the integrity of the application, it is important that there should be only one instance of this object and the above solution doesn’t offer such solution.

Another approach suggested by Bennett and Farmer (2002) through creating the company class via the singleton pattern that will overcome the above problems, where the company attributes will be encapsulated within the company class, and also to ensure that there is only one instance of this object to maintain the system integrity. That said; the application should be constructed so that it is impossible to create more than one instance of such class. To achieve such goal the company class that implements the airline information should have a sole responsibility for creating the company object and as such; the class constructor must be declared private so that it is not accessible by another object:

Company Class

-companyInstance-companyName companyRegistrationNumber
+getCompanyInstance() +getCompanyDetails() -Company

The creation of the company object occurs when the company class receives the message via getCompanyInstance() operation where it invokes the company class instructor. Also when the company object is first created, its attribute values will be populated from the database.

Finally, by implementing the singleton approach, we’re able to populate the airline company information globally within the application without violating any encapsulation rules, and also maintain one instance of the class that populate the company attributes that caries the company’s information, and also such implementation ensure that the class instantiation operation is a thread safe since only one instant of the class can be instantiated.

Singleton Pattern in C#

Lippert (2010) explained that a singleton is a class where it allows a single instance of such class to be created and gives an easy access to that instance. In most cases, the singleton class doesn’t allow any parameters to be specified, and there are a various ways of implementing such design pattern. All the following implementation has the following common characteristics:

  • The pattern implement a single constructor that is private and parameter-less where it prevents other classes from instantiating it and violate the pattern rules.
  • The class in the singleton pattern is sealed, where it allows the JIT (Just-in-Time code) to be optimized.
  • A Static variable that holds a reference to the single created instance of the class.
  • Public static property Instance is implemented to access the instance. Such property can be converted to a method with no impact on performance or on the thread-safety.

EX.

The software design patterns are generally classified into three categories: Behavioral Patterns, Structural Patterns, and Creational Patterns where it deals with the best method of creating objects. The Singleton Pattern is a good example of Creational Pattern. It is used when only one instance of an object is needed to be populated during the lifetime of the application. The singleton class is instantiated at the first time the application is accessed and the same instance is used throughout the application until the application quits (Rajesh, 2003).

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 voidMain()

{

SingleInstanceClass sic 1 ,sic2;

sic 1 = SingleInstanceClass.Create();

if(sic 1 != null) Console.WriteLine(“OK”);

}

sic2 = SingleInstanceClass.Create();

if(sic2 == null)

Console.WriteLine(“NO MORE OBJECTS”);

}

Conclusion

Design patterns provide solutions for many software development problems. One of these patterns is the singleton patter where it allows a class to have only one single instance, and provides a global access point to such class within the application. The class that implements such pattern is responsible to maintain and keep track of only one instance of itself rather than relying on a global variable to keep track of the class instances. Such pattern is used in a situation where it is required to hold a part of the information within the system where it can only be a single instant within the application lifetime (Mccutchen, 2007).

References

Bennett, S. & Farmer, R. (2002) Object-Oriented Systems Analysis and Design Using UML. 2nd ed.UK: McGraw- Hill Education.

Lippert, E. (2010) C# in Depth: Implementing the Singleton Pattern in C# [Online]. Available from: http://csharpindepth.com/Articles/General/Singleton.aspx (Accessed: 15 March 2011).

Mccutchen, R. (2007) Design Patterns in C# – Singleton Design Pattern [Online]. Available from: http://dotnet.dzone.com/articles/design-patterns-singleton‑

c?utm source=feedburner&utm medium=feed&utm campaign=Feed%3A+zones%2Fdotnet+(.NET+Zone)  (Accessed: 15 March 2011).

Rajesh, V. (2003) Creational Patterns in C# [Online]. Available from: http://www.devarticles.com/c/a/C­Sharp/Creational-Patterns-in-C-sharp/ (Accessed: 15 March 2011).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: