Posts

Showing posts with the label Design Pattern

Singleton Design Pattern

one of the good explanation found in ASP.NET 3.5 Application Architecture Design public sealed class EmailManager { private static EmailManager _manager; //Private constructor so that objects cannot be created private EmailManager() { } public static EmailManager GetInstance() { // Use 'Lazy initialization' if (_manager == null) { //ensure thread safety using locks lock(typeof(EmailManager) { _manager = new EmailManager(); } } return _manager; } } let us understand the code step-by-step: 1. public sealed class EmailManager: We have used the sealed keyword to make our EmailManager class uninheritable. This is not necessary, but there is no use having derived classes as there can be only one instance of this class in memory. Having derived class objects will let us create two or more instances which will be against the singleton's design objective. 2. private static EmailManager _manager: Next, we...