The singleton design sample is extensively utilized in object-oriented programming because it ensures that just one class occasion is created, and to entry it supplies a worldwide level of entry. Because of this each request for an occasion of the category returns the identical occasion, quite than creating a brand new one.
Singleton Design Sample in C#
The singleton sample is understood for its simplicity and effectiveness in making certain that there’s just one occasion of the category, making it a well-liked selection for a lot of software program builders. The singleton sample is usually used to make sure that a single level of entry exists to a shared useful resource or service, right here is the syntax for utilizing it:
{
personal static readonly <ClassName> occasion = new <ClassName>();
personal <ClassName>() {}
public static <ClassName> Occasion
{
get
{
return occasion;
}
}
}
The sealed key phrase is used to forestall the category from being inherited and the occasion area is marked as read-only to make sure that it may solely be initialized as soon as, and it’s initialized at declaration with a single occasion of the category. The constructor is marked as personal to forestall exterior instantiation of the category and the Occasion property supplies a method to entry the one occasion of the category and is marked as static in order that it may be accessed with out creating an occasion of the category.
In C# A singleton sample is usually applied utilizing a non-public constructor, a static occasion area, and a public static property or methodology to entry the occasion. The essential concept is to make sure that just one occasion of the category can exist by controlling how it’s instantiated and accessed, right here is an instance code that demonstrates the creation and use of the singleton design sample in C#:
/*Outline the Calculator class with a non-public constructor and a static occasion area*/
public sealed class Calculator
{
personal static Calculator occasion;
personal Calculator() { } /* Non-public constructor prevents instantiation of Calculator class from exterior the category itself*/
/*Outline the Occasion property, which creates a brand new Calculator object if one would not exist already and returns it*/
public static Calculator Occasion
{
get
{
if (occasion == null) /* Verify if a Calculator object has already been created*/
{
occasion = new Calculator(); /* If not, create a brand new Calculator object and assign it to the occasion area*/
}
return occasion; }
}
public int Add(int x, int y)
{
return x + y;
}
/* Outline the Subtract methodology, which subtracts one integer from one other and returns the consequence*/
public int Subtract(int x, int y)
{
return x – y;
}
}
// Outline the Program class with a Primary methodology
class Program
{
static void Primary(string[] args)
{
Calculator calc = Calculator.Occasion; /* Get an occasion of the Calculator object utilizing the Occasion property*/
int result1 = calc.Add(5, 7); /* Use the Add methodology so as to add 5 and seven collectively*/
Console.WriteLine($“Result1: {result1}”); /* Output the results of the addition to the console*/
int result2 = calc.Subtract(10, 3); /* Use the Subtract methodology to subtract 3 from 10*/
Console.WriteLine($“Result2: {result2}”); /* Output the results of the subtraction to the console*/
}
}
This instance defines The Calculator class with a non-public constructor and a static occasion area. The personal constructor prevents the category from being instantiated from exterior the category itself, whereas the static occasion area ensures that just one occasion of the category can exist at a time.
The Calculator class additionally contains an Occasion property, which creates a brand new Calculator object if one doesn’t exist already and returns it. The primary time the Occasion property known as, it creates a brand new Calculator object and assigns it to the occasion area, any subsequent calls to the Occasion property merely return the present Calculator object.
The Calculator class additionally contains Add and Subtract strategies, which carry out primary arithmetic operations and return the consequence. These strategies are simply included for instance of the sort of performance that the Calculator class may present.
Lastly, the Program class features a Primary() methodology, which creates an occasion of the Calculator class utilizing the Occasion property and makes use of the Add and Subtract strategies to carry out some easy calculations. The outcomes of those calculations are then output to the console utilizing the Console.WriteLine() methodology.
If we try to create a brand new occasion of the Calculator class utilizing the default constructor. This won’t work as a result of the constructor of the Calculator class is personal, which prevents new cases from being created exterior of the category.
To show I created a brand new occasion of the Calculator class utilizing the Occasion property of the category and within the output, there can be an error that states this class is inaccessible attributable to its safety stage:
By demonstrating that new cases are rejected by the singleton sample in C#, this instance reveals how the singleton sample ensures that just one class occasion is created and maintained all through this system.
Conclusion
The singleton sample is usually utilized in conditions the place a single object must coordinate actions throughout the system, comparable to a logging service or a database connection. The sample is applied utilizing a non-public constructor and a static property that returns a single occasion of the category, this text offers an in depth clarification about this sample.