简体   繁体   中英

C# - is it possible to implement the singleton pattern by using an interface?

I'm currently developing an application in which I'm using a plugin system. For providing unified access to a configuration screen I added a settings class to each plugin which must implement a settings interface. Furthermore each Settings class should implement the singleton pattern as shown below:

public sealed class PluginSettings : IPluginSettings
{

    private static readonly PluginSettings instance = new PluginSettings();
    private PluginSettings () { }

    public static PluginSettings Instance
    {
        get
        {
            return instance;
        }
    }

    # region interface implementation
    # ...
    # endregion

}

Is it possible to implement the singleton pattern already in the interface?

Any help appreciated - thanks in advance!

You could optionally use an abstract class instead of an interface and implement the singleton in the base abstract class.

You can use generics to make the singleton instance of the type of the inheriting class.

Sorry no. Interfaces don't have implementation.

You could have Generic interface, something like:

public interface Singleton<T> 
{
    T Instance { get; }
}

You can't enforce it through an interface, because if you had an interface like:

public interface ISingleton
{
  ISingleton GetInstance();
}

Firstly, the interface only covers the instance methods, not static, which is what you want for the singleton pattern. Secondly, there is nothing to enforce that GetInstance returns a singleton; it could, but it could also return a new object each time, a pooled object, and so on.

This is reasonable, the singleton is an implementation pattern, more than a overall design pattern (one reason why its often considered an anti-pattern in most cases), so there's little value in having an interface (in the general sense) promise an implementation, much better to have the interface (again, in the general sense) promise to return a usable instance, and leave the implementation up to the class in question.

You can extend your interface as follows:

public interface IPluginSettings
{
    IPluginSettings Instance
    {
        get;
    }
}

and in the concrete PluginSettings class implement some logic of the get method.

It seems that you'd better have a factory to construct something (Singleton) that implements the interface.
UPDATED
You can use IoC like Ninject and bind the interface to constant. Try:

Bind<IPluginSettings>().ToConstant(PluginSettings);

I have not tried it by maybe you can create an interface like

public interface ISingleton<T>
{
    T Instance
    {
        get;
    }
}

A generic singleton class

public class Singleton<T> where T : new (){
        Singleton() { }

        public static T Instance
        {
            get { return SingletonCreator.instance; }
        }

        class SingletonCreator
        {
            static SingletonCreator() { }

            internal static readonly T instance = new T();
        }
}

And then

public class xxx: ISingleton<xxx>{
     public Singleton<xxx> Instance...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM