Inheritance and Reusebility for Singleton Pattern using C#

0
74

Introduction

The Code snippet shows how to inherit from a singleton class using an simple parametrized constructor.

Background

Basic knowledge of Singleton design pattern is required for better understanding.

Using the code

This code can be used in a console application Project. The code has 3 classes which are mulilevel Inherited. The classes are:

1. public class MySingletonBase 
2. public class MySingleton : MySingletonBase
3. public class MySingletonChild : MySingleton

The MySingletonBase class is included just to show that a base constructor is always invoked before the instantiation of a child class. The code in MySingletonBase class is as follows:

public class MySingletonBase
{
 protected MySingletonBase()
 {
        Console.WriteLine("(MySingletonBASE) Constructor Invoked");
    }
 protected void MySingletonBaseMethod()
    {
        Console.WriteLine("(MySingletonBASE) Method Called from (MySingleton)");
    }
}

MySingletonBase class is inherited by MySingleton class, Here MySingletonPrivateMethod() is included to show that if any encapsulated method needs to be invoked it could be done in the constructor. Also a simple overloaded constructor is included to make inheritance possible.

public class MySingleton: MySingletonBase
{
 private static MySingleton MySingletonInstance = null;
    private MySingleton()
    {
        Console.WriteLine("(MySingleton) Constructor Invoked ");
        MySingletonPrivateMethod();
        MySingletonBaseMethod();
    }

    protected MySingleton(bool Invoke) 
    {
        Console.WriteLine("(MySingleton) Parameterized Constructor Invoked ");
    }

    protected virtual void MySingletonPrivateMethod()
    {
        Console.WriteLine("(MySingleton) Private method Invoked from Constructor");
    }
    protected  void MyNewPrivateMethod()
    {
        Console.WriteLine("(MySingleton) Protected member called from child (MySingletonChild)");
    }
   public static MySingleton GetObjectChild()
    {
        if (MySingletonInstance == null)
        {
            MySingletonInstance = new MySingleton();
            Console.WriteLine("(MySingleton) New Instance Created");
        }
        else
        {
            Console.WriteLine("(MySingleton)  Returning Existing Instance ...");
        }
        return MySingletonInstance;
    }
    
}

The below code shows that MySingletonChild class inherits from MySingleton, Here the overloaded constructor of the parent (MySingleton) is called on child (MySingletonChild) instantiation. The static GetObjectChild() method return a single instance of the singleton class.

public class  MySingletonChild : MySingleton
{
        private static MySingletonChild MySingletonChildInstance = null;
        private MySingletonChild() : base(true)
        {
            Console.WriteLine("(MySingletonChild) Constructor Invoked");
            
           
        }
        protected override void MySingletonPrivateMethod() 
        {
            Console.WriteLine("(MySingletonChild) overrides parent (MySingleton) method");
        }
public static new MySingleton  GetObjectChild()
        {
            if (MySingletonChildInstance == null)
            {
                MySingletonChildInstance = new MySingletonChild();
                Console.WriteLine("(MySingletonChild) New Instance Created");
            }
            else
            {
                Console.WriteLine("(MySingletonChild) Returning Existing Instance  ...");
            }
            return MySingletonChildInstance;
        }
        
    }
}

The console app Main code is:

static void Main(string[] args)
{
 Console.WriteLine("Create Singleton of MySingleton Class...");
            MySingleton.GetObjectChild();
            Console.ReadLine();

     Console.WriteLine("Create Singleton of MySingletonChild Class...");
            MySingletonChild.GetObjectChild();
            Console.ReadLine();

            Console.WriteLine("Creating 2nd Instance (MySingletonChild)");
            MySingletonChild.GetObjectChild();
            Console.ReadLine();
}

The output is :.

Points of Interest

Reusebility & Multilevel Inheritance of Singleton Class.

History

LEAVE A REPLY