I have an interface to describe when a class can create a "next" version of itself:
public interface Prototypeable<Type extends Prototypeable<Type>> {
public Type basePrototype(); // the zeroth raw instance of Type
public Type nextPrototype(); // the next instance of Type
}
to be used with
public class Prototyper {
public static <Type extends Prototypeable<Type>> List<Type> prototypeFactor(int numberOfInstances, Type proto) {
List<Type> result = new ArrayList<Type>(numberOfInstances);
Type holder = proto.basePrototype();
result.add(holder);
for (int i=1; i<numberOfInstances;i++) result.add(holder = holder.nextPrototype());
return result;
}
Now, I have a base class A implements Prototypeable<A>
, and an subclass AButMore extends A
. I would like to have AButMore extends A implements Prototypeable<AButMore>
, but this isn't allowed (cannot implement generic interfaces multiple times with different classes). Also note that A
and AButMore
both implement some other interfaces, and that implementation is identical from A
to AButMore
.
Suggestions for getting around this? I can't seem to fiddle around the generic problem, so I've considered a few alternate designs:
pseudo-decorating both classes - i.e., having a base class that doesn't implement the
Prototypeable
interface, inheriting from that to the proper subclass and then having both classes extended to Prototypeable versions of themselves. The downside seems to be a profusion of classes.not extending
A
toAButMore
and instead constructingAButMore
fromA
s and delegating all the replicated methods. However, delegate code always seems silly to me, especially when every method that could be inherited is going to be delegated with no modifications.having
Prototypeable
specifyObject
as the return type, and having the factory take aClass
parameter for casting. The downside here is that this can allow for unsafe casts if used improperly.
EDIT: To clarify: the intent is to manufacture instances that have some sort of sequential dependency, without having a class variable. The simplest example would be if they each have an index variable - basePrototype would provide a 0-index instance, and nextPrototype() would provide an index+1 instance (based on the index of the instance that the method was called from). That particular case is a little simplistic (and probably could be implemented in a simpler fashion), but covers the idea.
EDIT: For further clarification, here is the exact current implementation (I am using the third alternative above):
public class BuildFromPrototype {
public static <T extends Prototypeable> List<T> build(int buildCount, Class<T> protoClass, T prototype) {
if (protoClass==null || prototype==null || buildCount<=0) return null;
if( protoClass.isInstance(prototype.basePrototype()) && protoClass.isInstance(prototype.nextPrototype()) ) {
List<T> result = new ArrayList<T>(buildCount);
T pHolder = protoClass.cast(prototype.basePrototype());
result.add(pHolder);
for (int i=1;i<buildCount;i++)
result.add(pHolder = protoClass.cast(pHolder.nextPrototype()));
return result;
} else return null;
}
public interface Prototypeable {
public Object nextPrototype();
public Object basePrototype();
}
}
I think this handles misuse (returning null
is one option, an Exception
would have also been reasonable), but testing for valid casts could be expensive. This form of casting might also be expensive - I don't know much about the Class
class.