tags:

views:

58

answers:

2

how do i (and does it makes any sense?) inherit from the singleton class other classes that need the same functionality?

A: 

Someone feel free to correct me, but the way I understand it, and had an error with this once:

public class BaseClass
{
  protected static List<string> ListOfSomething { get; set; }
}

public class ChildClass
{
  protected static List<int> ListOfSomethingElse { get; set; }
}

public class AnotherChildClass
{
  protected static List<int> ListOfSomethingElse { get; set; }
}

Both of the child classes would share the same ListOfSomething, they would not have their own copy. The same static one would be shared amongst all children. This is the molt of singleton behavior and inheritance. As silky said...you just shouldn't do it, you'll probably run into something along these lines.

If you're not talking about something like this...I'm not sure what singleton you're speaking of, and an example would help greatly, since singleton's have a great deal of niche uses.

Nick Craver
A: 

Jon Skeet wrote about this a while back. It is possible to achieve some of the benefits of inheritance with the Singleton, although using nested inner classes does leave a little to be desired. It doesn't have infinite extensibility, it's only a technique for having a Singleton choose its own implementation at runtime.

Realistically, inheriting from a Singleton doesn't make all that much sense, because part of the Singleton pattern is instance management, and once you already have a physical instance of a base type then it's too late to override any of this in the derived type. Even if you could, I suspect that it could lead to a design that's difficult to understand and even more difficult to test/maintain.

Aaronaught