What is the Singleton Design Pattern?
A group of authors immediately affectionately understood as the 'Gang of Four' (Erich Gamma, Richard Helm, Ralph Johnson, and John Vilssides) wrote a paperback cried 'Design Patterns: Elements of Reusable Object-Oriented Software'. In this writing they defined 3 categories of design patterns which are:
- Creational Patterns
- Structural Patterns
- Behavioural Patterns
The Singleton Design pattern falls into the classification of Creational Design Patterns, the Singleton Design Pattern ensures your software has only an instance of the class and provides a universal way to it. In other words the Singleton design pattern is always approximately making sure namely you can instantiate only one object of a particular class. If you don't use a pattern like this one, the current worker just keeps on creating more and extra objects.
Could I not just use a Static Class
Technically you could equitable use a static class for 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 idle fraught. Only when it namely actually needed. That's very convenient if the initialisation includes valuable resource loading or database connections
- Singeltons offer an actual object
- Singletons can be amplified into a software factory. The object treatment backward 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 merely 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 at using a new operator, case in point:
C#: MyObject myNewInstance = new MyObject()
VB.NET: Dim myNewInstance As New MyObject()
Each time a new object the microprocessor allocates a heap of memory, creating lots of these object will amplify the measure of memory is used and potentially achieve the representation 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 direction 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 new instance.
Singleton Design Pattern Example
C#:
public class MyObject
{
//create a private static variale to store our instance
personal 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 yet have one instance, if so return it, another build 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 allege 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 penetrate if an instance exists in that variable, if I were you one is created and returned.
没有评论:
发表评论