Skip to main content

Posts

Showing posts with the label lazy

VBA versus .NET

Singleton (Update)

I recently came across a new way of implementing the Singleton pattern, detailed in this article . The article details, (1) the use of the volatile keyword, and (2) a new simpler way of implementing a Singleton by using the Lazy<T> class , something that was not available when I first wrote the section on the Singleton .

Singleton

The Singleton design pattern seems generally frowned upon, except for use in logging classes.  Below are several variants of the Singleton design pattern, of which, only the thread-safe version merits attention: Salient Chacteristic(s) A private constructor Static variable for self-tracking Three (3) variants Eager Lazy Thread-safe (Double-checked Locking) Code namespace DesignPatterns {     //double-checked locking singleton     //required for threaded environments     public class DoubleCheckingSingleton     {         private static volatile DoubleCheckingSingleton _Instance = null;         private static object _SyncRoot = new object();              //Private constructor prevents instantiation from other classes         private DoubleCheckingSingleton()         {        ...