I am very new to Java generics and have spent an exorbitant amount of time looking for an appropriate solution (if any).
I’m trying to design adapters that handle objects of a specific type of class. As described below, the CAdapter class handles “CClass” objects only. I am trying to provide an abstract generic adapter that handles the bulk of the work (much like Java collections such as LinkedList). I then provide a concrete adapter implementation for each type that needs to be supported.
// Classes
public interface AInterface {
public String toString();
}
public class BClass extends AInterface {
public String toString() { return "BClass "; }
}
public class CClass extends AInterface {
public String toString() { return "CClass"; }
}
// Adapters
public interface AdapterInterface<T extends AInterface> {
public T getInterface();
}
public class BAdapter implements AdapterInterface<BClass> {
private BClass aInterface = null;
public BClass getInterface() { return aInterface; }
}
public class CAdapter implements AdapterInterface<CClass> {
private CClass aInterface = null;
public CClass getInterface() { return aInterface; }
}
Firstly, I have read that providing a CONCRETE implementation for such a generic adapter is frowned upon (something about God killing a kitten)! Maybe somebody could expand on this?
Secondly, I have run into an issue with dynamically instantiating an adapter and not having the Java compiler complain. For example, I have the method:
public <T extends AInterface> AdapterInterface<T> getAdapter(String type) {
AdapterInterface<T> result = null;
if (type.equals("C") {
result = new CAdapter();
}
return result;
}
Of course, the compiler will complain about CAdapter not matching . Given any type of AInterface object I would like to be able to load the right adapter and process it appropriately. I am failing to understand the factory pattern in order to accomplish this.
Any thoughts would be greatly appreciated.
My answer is a bit superfluous, but:
Anything with a <T> means “my caller knows what this type is, but I don’t”. So
Means “I don’t actually know what type result is, it’s whatever my caller thinks it is”. The compiler complains about this:
Because the this code can’t assume that T is CClass.
In fact, there is no way to do this without a cast (declaring the method wild carded just means that you need to cast the result where you call it). The cast is your way of telling the compiler “I know that you have no way of knowing what this is, that’s ok: I do. Trust me. Chill.”. Yes, you’ll get a warning. And that’s ok.
Generics don’t eliminate all casting, but they allow you to do it just once. Instead of needing casts all over the place, you need them at just that one spot where you – the coder – know for sure that you are playing games with types. All the rest of the code, the suff that uses the adapter you have just created, can safely work with the generic type.