The last design pattern that I want to mention in Creational Category is the Singleton Design Pattern. So, let’s see what is it and how we can implement this pattern in our program.
Note: Code can be downloaded at my Github
1. What is the Singleton Design Pattern?
As the name suggests, Singleton pattern is a way to control the number of instance of a class to be only one. By doing so, the application is able to provide a global access point to that single instance.
The Singleton Design Pattern is a Creational Design Pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
2. When to implement the Singleton Design Pattern?
The motivation to use Singleton is when your applications have some components which only make sense to have one instance in the system such as a Database Repository or an Factory Object.
The components above are examples of instances that should be created one only and everyone can access to the same instance.
The Singleton Design Pattern helps to prevent anyone creating additional copies of the instance, take care of the lazy instantiation and thread safety.
3. How to Implement Singleton Design Pattern?
We can implement Singleton Design Pattern for the Database class as we mentioned in above example here.
Step 1: Create classes for Entities
To simplify, we take a User class as an example.
public class User { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"Name: {Name}, Age: {Age}"; } }
Step 2: Create the Singleton interface and class
We are implement Singleton for Database, therefore, we create an interface IDatabase and a class SingletonDatabase:
public interface IDatabase { User GetUserByName(string name); } public class SingletonDatabase : IDatabase { private string database; private SingletonDatabase() { Console.Write("Initializing database"); Console.WriteLine(); database = "[ { \"Name\": \"Lucas\", \"Age\": 29 }, { \"Name\": \"Eric\", \"Age\": 28 } ]"; } private static SingletonDatabase instance = new SingletonDatabase(); public static SingletonDatabase Instance = instance; public User GetUserByName(string name) { Listusers = JsonConvert.DeserializeObject<> >(database); return users.Where(u => u.Name == name).FirstOrDefault(); } }
Notice that the constructor for SingletonDatabase class is private and there is a public member called Instance to represent the single instance of this Database.
There is a function called GetUserByUserName which get user data by filtering the JSON string database. Note that the JSON string database represents the database in this example and it is initiated only once in the private constructor.
On the client side, we can run below code to get user by using GetUserByUserName function:
class Program { static void Main(string[] args) { var db = SingletonDatabase.Intance; var user = db.GetUserByName("Lucas"); Console.WriteLine(user.ToString()); var db2 = SingletonDatabase.Intance; var user2 = db2.GetUserByName("Eric"); Console.WriteLine(user2.ToString()); } }
In the above code, even when we create a second database object called db2, it is referred to the only database instance that we created before. The "Initializing database" message is printed once only.
Done! This is how the Singleton Prototype is implemented basically. The actual implementation in real world applications maybe more complicated but I hope you get the idea of how to implement Singleton in your project.