I would like to be able to create classes and retain a little more control:
and in A:
The idea is that each of the subclasses has an explicit method that will create an instance but the creation is controlled by A.
For this to work, however, I need to force all subclasses to implement 'static X newInstance();' But I don't see a way to do this. 'static' isn't allowed in interfaces and I can't seem to make a 'static abstract' method in A.
Static methods cannot be inherited, because they belong to the class. Therefore there is no way for your class to force the implementation of a static method.
However, you could make the constructor private or protected, forcing the instantiation of object through a static method you have provided.
This method may have the same name as that you define in the base class, however, you must be really careful and understand that this is in no way an inheritance mechanism. You would simple be hiding the method of the base class with one define in the child class.
Therefore if you do somewhat like this:
In the following code:
Therefore if you ever do this, try to access the static methods using the class identifier and not a particular instance of it, because you can get ackward behavior difficult to spot and debug.
This sounds like a bad idea. The syntax of the language doesn't support what I am trying to accomplish.
I think I'll go with plan B:
This allows me to control the creation of the instance while simultaneously making a single place where the creation happens. Now if I needed to add security, object caching :roll: , or some other higher level management I could do it.
Why? It's a compromise. [ October 18, 2006: Message edited by: Tony Smith ]