Using the singleton design pattern
What is the Singleton Design Pattern?
A group of authors now affectionately known as the ‘Gang of Four’ (Erich Gamma, Richard Helm, Ralph Johnson, and John Vilssides) wrote a book called ‘Design Patterns: Elements of Reusable Object-Oriented Software’. In this book they defined three categories of design patterns which are:
- Creational Patterns
- Structural Patterns
- Behavioural Patterns
?
The Singleton Design pattern falls into the category of Creational Design Patterns, the Singleton Design Pattern ensures your software has only one instance of the class and provides a global access to it. In other words?the Singleton design pattern is all about making sure that you can instantiate only one object of a particular class. If you don’t?use a pattern like this one, the new operator just?keeps on?creating more?and more objects.??
Could I not just use a Static Class
Technically you can only use a static class it will only have a common instance of a class. However, if you do not, then it may miss some design patterns for single persons, some of the benefits. These benefits are single people, including a static class are:
- Singletons can implement interfaces and inherit from other classes
- A singletons can be lazy loaded. Only when it is actually needed. That’s very handy if the initialisation includes expensive resource loading or database connections
- Singeltons offer an actual object
- Singletons can be extended into a software factory. The object management behind the scenes is abstract so it’s better maintainable and results in better code
- Static classes are instantiated at runtime. This could be time consuming. Singletons can be instantiated only when needed
So how do you use the Singleton Design Pattern?
In object-oriented programming and software development, you typically create an instance of an object with a new operator, for example:
C#:??????????? MyObject myNewInstance = new MyObject()
VB.NET:????? Dim myNewInstance As New MyObject()
Each time a new object of the computer has a heap of memory, creating lots of these items will increase the amount of memory used and may result in the performance of your software product.
So how do we do this so that we only ever one instance of our class, but this is a very simple 2 step process:
- Create the objects contructor as PRIVATE , that way no code outside of the class can creat an instance of the object.
- Create a static/shared method within the class that will return a newly created instance of your class, or if an instance already exists then it will return the current instance.
?Singleton Design Pattern?Example
C#:
public class MyObject
{
?????? //create a private static variale to store our instance
?????? private static MyObject singletonObject;
?????? //create a private constructor
???????private MyObject()
????? {
???????????? //initial
?????? }
?????? //create a publi static method to return the instance
?????? public static MyObject CreateInstance()
?????? {
/ / Check if we have an example, if the returns in, otherwise create a new
????????????? //instance
????????????? if (singletonObject == null)
???????????????????????? singletonObject = new MyObject();
??????????????return?singletonObject;
???????}
}
?
VB.NET :
Public Class MyObject
?????? ‘create a private static variale to store our instance
?????? Private Shared?singletonObject As MyObject
?????? ‘create a private constructor
???????Private Sub New()
??????
???????????? ‘initialise the object
???????End Sub
?????? ‘create a public static method to return the instance
?????? Public Shared Function CreateInstance() As MyObject
???????
????????????? ‘check if we already have an instance, if so?return it, else create a new
????????????? ‘instance
????????????? If singletonObject Is Nothing?Then
???????????????????????? singletonObject = new MyObject()
????????????? End If
??????????????Return?singletonObject
???????End Function
End Class
?
Please note that in a single design model, we declare a private static variable to store instances of a class of our implementation. When we call we have created, and create an instance of our checks to see if there is an instance of the variable, if there is no one is to create and return to function.

Leave a Reply