Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use interface with singleton class

Tags:

c#

As I go through the differences between Singleton Vs Static class, I came across one point that we can inherit an interface in singleton class and can call singleton through interface for multiple implementation.

I would like some code demonstration with good example, how object orientation can achieve through singleton and not through static.

Thanks,

like image 259
user584018 Avatar asked Aug 28 '13 12:08

user584018


3 Answers

Although it's hard to tell what exactly you are referring to, one pattern you might be referring to is the Multiton pattern, where you manage a map of named instances as key-value pairs.

That's basically a factory, but each instance is only created once:

I've modified the Wikipedia example a bit to show that you can even derive from a singleton class, as long as your concrete implementations are private and within the original class:

class FooMultiton
{
    private static readonly Dictionary<object, FooMultiton> _instances =
        new Dictionary<object, FooMultiton>();

    // this is the classic good old singleton trick (prevent direct instantiation)
    private FooMultiton()
    { }

    // you can also have private concrete implementations, 
    // invisible to the outside world
    private class ConcreteFooMultitonOne : FooMultiton
    { }

    public static FooMultiton GetInstance(object key)
    {
        lock (_instances) 
        {   
            FooMultiton instance;

            // if it doesn't exist, create it and store it
            if (!_instances.TryGetValue(key, out instance))
            {
                // at this point, you can create a derived class instance
                instance = new ConcreteFooMultitonOne();
                _instances.Add(key, instance);
            }

            // always return the same ("singleton") instance for this key
            return instance;
        }
    }
}

Also, generally, if a singleton is not a static class, it can implement any interface you want. The only thing that a singleton pattern prevents is instantiation of multiple instances of a singleton class, but that doesn't mean you cannot completely replace the implementation with something else.

For example, if you have a singleton which is not a static class:

interface ICanTalk
{
    string Talk();
}

class Singleton : ICanTalk
{
    private Singleton() { }

    private static readonly Singleton _instance = new Singleton();
    public static Singleton Instance
    { get { return _instance; } }

    public string Talk()
    { return "this is a singleton"; }
}

You can also have a number of different implementations:

class OtherInstance : ICanTalk
{
    public string Talk()
    { return "this is something else"; }
}

Then you are free to choose any implementation you want, but get only a single instance of the Singleton class:

ICanTalk item;

item = Singleton.Instance;
item = new OtherInstance();
item = new YetAnotherInstance();
like image 91
Groo Avatar answered Oct 16 '22 23:10

Groo


According to nkr1pr

Every class can implement an interface, and a Singleton is just a "normal" class that makes sure that only one instance of it exists at any point in time apart from the other business logic it may implement. This also means that a Singleton has at least 2 responsibities and this is not good OO design as classes should only have 1 responsibility and make sure they are good at that responsibility, but that is another discussion.

Something like:

public interface MyInterface 
{
}

And

public class MySingleton:MyInterface
{
  private static MyInterface instance = new MySingleton();

  private MySingleton() 
  {
  } 

  public static MyInterface getInstance()
  {
    return instance;
  }
}
like image 30
One Man Crew Avatar answered Oct 16 '22 21:10

One Man Crew


I'm not sure what you are asking, but singleton classes can implement interfaces. singleton class does not mean static class, one of the method to create a singleton instance is to make use of static members.

public class MyInterfaceImplementation : IMyInterface
{

    private static MyInterfaceImplementation instance;
    private static readonly object lockObj = new object();

    private MyInterfaceImplementation() { }  //private .ctor

    public static MyInterfaceImplementation Instance
    {
        get
        {
            if (instance == null)
            {
                lock (lockObj)
                {
                    instance = new MyInterfaceImplementation();
                }
            }
            return instance;
        }
    }

    public void MyInterfaceMethod()
    {
        //Implement here
    }
}
like image 1
Vignesh.N Avatar answered Oct 16 '22 22:10

Vignesh.N