rob,
Well, what you would do really at this point is expose a class factory
of sorts. Basically, since both singletons expose the same interface, you
have to have a way of choosing which implementation you want, something
like:
public interface IMyInterface
{}
public static class A : IMyInterface
{
static readonly instance = new A();
public static Instance
{
get
{
return instance;
}
}
}
public static class B : IMyInterface
{
static readonly instance = new B();
public static Instance
{
get
{
return instance;
}
}
}
public static class MySingletonFactory
{
public static this[string index]
{
get
{
if (index == "A")
{
return A.Instance;
}
if (index == "B")
{
return B.Instance;
}
throw new Exception();
}
}
}
It's a little sparse, but it should give you a good idea of what I am
talking about.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
rob said:
Yes, each of my singletons exposes a separate instance of itself.
Unfortunately, I don't really follow what your suggestion is. Could you
elaborate on this?
Thanks
Rob,
If you were truly implementing the singleton pattern, you would be
exposing an instance for each singleton. This would make exposing an
interface easy. You would just choose which singleton you are returning,
and return the interface implementation.
Of course, you do this instead of having static members.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
I have two different classes singleton1 and singleton2 that both are
singletons. Both classes implement the same functions. Therefore, I
would like to have an interface containing these functions. The problem
is that an interface does not allow static functions and an abstract
class does not work in my case because multiple inheritence is not
allowed. So how do I go after doing this?
Thanks