0

I want to have a number of similar children classes (singletons) derived from a base class (abstract preferably as the base doesn't need to be created). These children classes interact with one another and most importantly I want them to share a reference to a pool of particular data (not many).

It sounds like a common scenario, but I'm not sure what would be the best design? maybe my mind is a bit shot.

I don't believe its necessary to break the common/shared member out of the class structure, but I'm not sure how best to store/access it. I'd like to try to avoid passing it around, though functionally it may make sense.

Any suggestions would be appreciated.

user1297102
  • 661
  • 8
  • 14

1 Answers1

1

I would avoid using a static as suggested. I find it bad practice in most cases (Should you avoid static classes?).

An alternative could be to create a DataPool class (and IDataPool interface), and inject an instance (there is only one instance) into your singletons (singleton classes accept a IDataPool parameter).

This DataPool class would then provide all methods needed to interact with the pool data, and hold the data itself. In fact, it abstracts the way the data is accessed and as such is easy to reuse in your singleton classes.

You could use an IoC container to decide about lifetime, or create the instance somewhere else using the new operator.

This way it's also easy to mock out or replace the data pool (in your tests, for example).

Community
  • 1
  • 1
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • Thanks for the suggestion and for laying out the structure! and i understand that in programming as in life, moderation is key ;-) – user1297102 Apr 17 '13 at 18:14