views:

235

answers:

7

Hello,

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!

+6  A: 

Sorry no. Interfaces don't have implementation.

jdv
+9  A: 

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.

vc 74
I'll try it this way - all those solutions seem either oversized or not suitable for my problem. Gotta keep learning... ;-)
dhh
Keep the pressure, learning takes a lifetime... ;)
vc 74
+2  A: 

You could have Generic interface, something like:

public interface Singleton<T> 
{
    T Instance { get; }
}
Amittai Shapira
I think you misread the question.
Bobby
Hum, this does not seem to work as the "Instance" field defined for the singleton pattern is static. Any idea how to fix this?
dhh
A: 

Hello, Daniel.

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.

Eric Cartmenes
But then you can't get the PluginSettings until you have a PluginSettings, and you can't get that because you don't have a PluginSettings yet.
Jon Hanna
A: 

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);
StuffHappens
A: 

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...
}
p.revington
static implementation of interface won't match, it has to be implementated as an instance method, which means you can't get an instance until you have an instance.
Jon Hanna
Jon, you are right
p.revington
+1  A: 

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.

Jon Hanna