Skip to main content

VBA versus .NET

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()
        {
            //class contruction
        }
        
        public static DoubleCheckingSingleton Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (_SyncRoot)
                    {
                        if (_Instance == null)
                        {
                            _Instance = new DoubleCheckingSingleton();
                        }
                    }
                }
                return _Instance;
            }   
        }
    }

    //lazy initialization of singleton
    public class LazySingleton 
    {
        private static LazySingleton _Instance = null;
    
        // Private constructor prevents instantiation from other classes
        private LazySingleton() 
        {
            //class contruction
        }


        public LazySingleton getInstance() 
        {
            if (_Instance == null)
            {
                _Instance = new LazySingleton();
            }
            else
            {
                return _Instance;
            }
            return _Instance;
        }
    }


    //eager initialization of singleton
    public class EagerSingleton
    {
        private static EagerSingleton _Instance = new EagerSingleton();

        // Private constructor prevents instantiation from other classes
        private EagerSingleton()
        {
            //class contruction
        }

        public EagerSingleton getInstance()
        {
            return _Instance;
        }
    }
}

Comments

Popular posts from this blog

Decorator Pattern

This is an example of the Decorator pattern, in this case a decorator for ObservableCollection.  Working with WPF and ObservableCollection using threads, one will run into the problem whereby the ObservableCollection cannot be updated from outside the owning thread; Delegates and Invoke will not work. A solution is to contain and expand the class, as is done in this example on michIG's Blog . This linked file contains the original code in C#, as well as the same code converted to VB.NET. Salient Characteristic(s) Sets an internal pointer to the decorated object, sending method calls and property actions to the internal object Extends the object by wrapping it and adding some aspect handled by the decorator Code using System; using System.Collections.ObjectModel; using System.Windows.Threading; using System.Collections.Specialized; using System.ComponentModel; namespace DesignPatterns {     /// <summary>       /// This class is an...

James Igoe's Reviews > Thinking Architecturally

Thinking Architecturally by Nathaniel Schutta My rating: 4 of 5 stars An overview of architectural decisions, the politics and persuasion involved, and the needs to balance competing measures and attributes. A fairly easy read, but full of great suggestions, and, for many, reminders of how to handle being a senior developer or architect. View all my reviews

Migrating and Design Planning

I have been toying with the idea of migrating one of my sites to a better host - it was supported by Yahoo and now AAbaco - and implementing some newer technologies. Among products I have used at work or are working with peripherally, I am considering using ASP.NET MVC, Entity Framework, ReSTful API's, NoSQL, and Azure-hosted databases - it is currently a mixture of very low-end PHP, HTML5/CSS3, light Javascript, and MySQL - so I decided to write up an architectural diagram - it looks like any standard architecture, with maybe a few additional elements - to help with the planning: