Thanks for visiting Toy Dolls. Please remember to bookmark this page Scene Pattern. If you found what your looking for, then don"t forget to click an appreciation button above for this post.
Scene Pattern
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 could just use a static class as then there would only be one shared instance of a class for. However, if you did that then you would miss out on some of the benefits of the singleton design pattern. These benefits that singletons have over static classes include:
- 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 generally create an instance of an object by using a new operator, for example:
C#: MyObject myNewInstance = new MyObject()
VB.NET: Dim myNewInstance As New MyObject()
Each time a new object the computer allocates a heap of memory, creating lots of these object will increase the amount of memory is used and potentially effect the performance of your software product.
So how do we make it so that we only ever have one instance of our class, well it is a very simple 2 stage 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 already have an instance, if so return it, else 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
Notice how in the implementation of the singleton design pattern we declare a private static variable to store the instance of our class. When we call the function we have created to create our instance a check is performed to see if an instance exists in that variable, if not one is created and returned.
About the Author
About the Author: This article was provided by Real IT Solutions, a Derbyshire based Derby Web Design, Software Development and Internet Marketing company.

If you are looking for a different item here are a list of related products on Toy Dolls, please check out the following:



