In Java, if I want to know how many object of type MyClass, i can define the MyClass in this way
public class MyClass {
public static int count = 0;
public MyClass() {
count++;
}
//other stuff
//...
}
and then, just calling
MyClass.count
I can get the number of objects created.
I am wondering if there's a way to do the same thing with an interface, e.g. if I have my interface called ICountable, how can I know how many objects that are ICountable are there in my program at that moment. I am thinking of doing this with a factory pattern, but in any design way I notice weaknesses, so I haven't come up to a working solution yet, does anyone know a good way to do this?