How to use interface with singleton class

As I look at the differences between the Singleton Vs Static class, I come across one point that we can inherit an interface in a singleton class and can call singleton through an interface for multiple implementations.

I would like to demonstrate a code with a good example of how object orientation can be achieved through plain and not through static.

Thanks,

+7
c #
source share
3 answers

Although it's hard to say exactly what you mean, one template that you can reference is the Multiton pattern , where you control the map of named instances as key-value pairs.

This is basically a factory, but each instance is created only once:

I modified the Wikipedia example a bit to show that you can even get one class if your specific 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; } } } 

In addition, as a rule, if singleton is not a static class, it can implement any required interface. The only thing that prevents a singleton pattern is to create multiple instances of a singleton class, but that does not mean that 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 several different implementations:

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

Then you can choose any implementation you want, but get only one instance of the Singleton class:

 ICanTalk item; item = Singleton.Instance; item = new OtherInstance(); item = new YetAnotherInstance(); 
+4
source share

According to nkr1pr

Each class can implement an interface, and Singleton is just a β€œnormal” class, which ensures that only one instance of it exists at any time, except for the other business logic that it can implement. This also means that Singleton has at least 2 responsibilities, and this is not a very good OO design, since classes should only have 1 responsibility and make sure that they handle this responsibility well, but this 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; } } 
+4
source share

I'm not sure what you are asking, but singleton classes can implement interfaces. singleton class does not mean a static class, one of the methods for creating a singleton instance is to use static elements.

 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 } } 
+1
source share

All Articles