I want to create a set of classes that share a lot of common behavior. Of course in OOP when you think that you automatically think "abstract class with subclasses". But among the things I want these classes to do is to each have a static list of instances of the class. The list should function as sort of a singleton within the class. I mean each of the sub-classes has a singleton, not that they share one. "Singleton" to that subclass, not a true singleton. But if it's a static, how can I inherit it?
Of course code like this won't work:
public abstract A
{
static List<A> myList;
public static List getList()
{
if (myList==null)
myList=new ArrayList<A>(10);
return myList;
}
public static A getSomethingFromList()
{
List listInstance=getList();
... do stuff with list ...
}
public int getSomethingFromA()
{
... regular code acting against current instance ...
}
}
public class A1 extends A
{
...
}
public class A2 extends A
{
...
}
A1 somethingfromA1List=(A1) A1.getSomethingFromList();
A2 somethingfromA2List=(A2) A2.getSomethingFromList();
The contents of the list for each subclass would be different, but all the code to work on the lists would be the same.
The problem with the above code is that I'd only have one list for all the subclasses, and I want one for each. Yes, I could replicate the code to declare the static list in each of the subclasses, but then I'd also have to replicate all the code that adds to the lists and searches the list, etc, which rather defeats the purpose of subclassing.
Any ideas on how to do this without replicating code?
Update
To clarify for BlueRaja and perhaps others, let me be a little more specific about what I'm trying to do.
I have a number of statuses and codes that require similar behavior. They are like enums in that I want type-safe instances for each possible code state, but they come in multiple sets so I cannot just make one enum for them all.
So for example I have order status, that has values for on-order, shipped, received, and some special values. I have item type, with values for merchandise versus service. I have stock versus custom. Etc.
If that was all there was to it I'd just make an enum for each. But there's other behavior I need, like ability to look up an object by a text value, e.g. when we read it from the database; building a drop down listing all the choices by a text description; etc. To the best of my knowledge, I can't do this with an enum because I have several sets of codes each with common behavior that I want to inherit, and I can't subclass an enum. Putting all the codes in a single enum would abandon type safety on one code versus another, as well as proving no ready way to collect the list of values for a particular code type.