4

I am having a lot of trouble choosing between using Singleton or Static for a class that contains state variables. I wanted the class object to instantiate and exist only as one.

I know both ways can store state variables. Static Class seems easy to deal with the variables as all methods will become static, which they can access the static variables without any further work.

However, this case is different for a Singleton. I have both kinds of methods; A kind that needs to access to the Singleton's Instance variable, and other that without any access to the Instance variable, which I can mark it static.

An Example:

/// <summary>Singleton.</summary>
    public sealed class Singleton
    {
        private static readonly Singleton instance = new Singleton(); /// <summary>Instance.</summary>
        public static Singleton Instance { get { return instance; } }

        private int integer; /// <summary>Integer.</summary>
        public int Integer { set { integer = value; } get { return integer; } }

        /// <summary>Constructor.</summary>
        private Singleton() { }

        /// <summary>TestA</summary>
        public void TestA(int val)
        {
            Integer = val;
        }

        /// <summary>TestB</summary>
        public static int TestB(int val)
        {
            return Instance.Integer * val;
        }

        /// <summary>TestC</summary>
        public static int TestC(int val)
        {
            return val * val;
        }
    }

From the example given above, there are three methods; TestA, TestB, and TestC.

  1. TestA is a non-static instance method that has access to its property.
  2. TestB is a static method, but accesses the Instance to get its properties.
  3. TestC is a static method that the instance has no use.

This begs the question:

  1. Should the Singleton contains only static methods, and access to its Instance properties and methods by going through the static Instance property? In other words, all methods are similar to TestB or TestC.
  2. Should the Singleton contains only non-static methods, regardless whether if it needs the Instance or not? All methods similar to TestA.
  3. Should the Singleton contains both mixed static and non-static (in this case, TestA, and TestB kind) methods? Which I believe it can get rather messy.
  4. If not, what should I do? Should I dump the idea of Singleton, and go with all static for every classes that is to be instantiated only once?

Edit: With similar question, should Singleton even contain any Static variables/field/properties beside the Instance?

Kei
  • 121
  • 1
  • 10

1 Answers1

4

You shouldnt mix up both patterns.

If you have an Singleton pattern the only static field should be the Instance(+ the getter). All your methods and fields should be accessible through the instance. If you mix it up it will only cause confusion.

If you choose the the static class pattern don't use a secret instance inside thats the job of .NET.

If you are not sure what pattern fits best for you, have a look into this Singleton-vs-Static article. It explains the pro's and con's of both of them: https://www.dotnetperls.com/singleton-static

Piranha
  • 801
  • 1
  • 8
  • 22