Re: Singleton & Interfaces



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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"rob" <rmdiv2000@xxxxxxxxx> wrote in message
news:1151346212.278317.89560@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"rob" <rmdiv2000@xxxxxxxxx> wrote in message
news:1151344941.710874.27140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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




.



Relevant Pages

  • Re: Singleton & Interfaces
    ... Understanding the Simple Factory Pattern ... Basically, since both singletons expose the same interface, you ... public static class A: IMyInterface ... If you were truly implementing the singleton pattern, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Singleton
    ... hide the singleton class wholesale in the implementation section of the ... The interface is of course easily replaced with an abstract class: ... property MyFirstProperty: Integer read GetMyFirstProperty write ... procedure MyFirstProcedure; virtual; abstract; ...
    (borland.public.delphi.non-technical)
  • Re: Windows Service in VB
    ... Your service needs to expose an interface via .Net remoting. ... and register the public class as a well-known singleton. ... You also need to create a WinForms app that can be run by logged-in users. ... if your client app just wants to call methods on the service's remoted ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Singleton & Self := in constructor
    ... > I want to enforce singleton behavior in my TObject derived ... > The basic idea is that clients can call the Constructor any ... But it is much simpler to just use an interface ...
    (borland.public.delphi.language.objectpascal)
  • Re: Singleton
    ... I don't usually go to a string factory or a list factory to ... If you have an abstract DOM interface, for example, and the implementation ... To me it's very clear to go to the DomFactory or singleton ... As mentioned in my posting you unit test a class separate from the ...
    (comp.object)