I am writing a method that should accept as its parameter an object of one of two types which do not share a parent type other than Object. For example, the types are Dreams
How about this:
interface ICrushable {
void crush();
}
utterlyDestroy(ICrushable parameter) {
// Very long crushing process goes here
parameter.crush()
}
utterlyDestroy(Dreams parameter) {
utterlyDestroy(new ICrushable() { crush() {parameter.crush();});
}
utterlyDestroy(Garlic parameter) {
utterlyDestroy(new ICrushable() { crush() {parameter.crush();});
}
New development should implement the ICrushable interface, but for the existing Classes, the parameter is wrapped in an ICrushable and passed to the utterlyDestroy(ICrushable) that does all the work.
You can implement a Haskell-esque Either-class in Java; something like this:
class Either<L,R>
{
private Object value;
public static enum Side {LEFT, RIGHT}
public Either(L left) {value = left;}
public Either(R right) {value = right;}
public Side getSide() {return value instanceof L ? Side.LEFT : Side.RIGHT;}
// Both return null if the correct side isn't contained.
public L getLeft() {return value instanceof L ? (L) value : null;}
public R getRight() {return value instanceof R ? (R) value : null;}
}
Then you let that method take something of type Either<Dreams, Garlic>
.
If you are going to treat them the same way in many places of your project, I suggest to wrap them in a class, something like an Adapter.
You could use an interface and adapt your types to it.
Interface:
public interface Crushable {
public void crush();
}
Example invocation:
public class Crusher {
public static void crush(Crushable crushable) {
crushable.crush();
}
}
Example adapter factory method:
public final class Dreams {
public static Crushable asCrushable(final Dream dream) {
class DreamCrusher implements Crushable {
@Override
public void crush() {
dream.crush();
}
}
return new DreamCrusher();
}
private Dreams() {}
}
The consumer code looks like this:
Dream dream = new Dream();
Crushable crushable = Dreams.asCrushable(dream);
Crusher.crush(crushable);
If you have many types to adapt, you could consider reflection. Here is an (unoptimized) adapter factory that uses the Proxy type:
public final class Crushables {
private static final Class<?>[] INTERFACES = { Crushable.class };
public static Crushable adapt(final Object crushable) {
class Handler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return crushable.getClass()
.getMethod(method.getName(), method.getParameterTypes())
.invoke(crushable, args);
}
}
ClassLoader loader = Thread.currentThread()
.getContextClassLoader();
return (Crushable) Proxy.newProxyInstance(loader, INTERFACES, new Handler());
}
private Crushables() {}
}
To the API consumer, this isn't that ugly:
Dream dream = new Dream();
Crushable crushable = Crushables.adapt(dream);
Crusher.crush(crushable);
However, as is usual with reflection, you sacrifice compile-time type checking.
Creating an Interface Crushable seems like the cleanest way to go. Is subtyping Garlic or Dreams an option, and adding your Interface to the subtype?
Barring that, you can put common code in a private method, and have the two versions of utterlyDestroy do what they have to do to the individual objects before calling the common code. If you method body is long, probably need to break it up into private methods anyway. I'm guessing you already thought of this, though, as it is even more obvious a solution than adding an Interface.
You can bring the parameter in as an Object and then cast it. Is this what you mean by reflection? i.e.,
public void utterlyCrush(Object crushable) {
if (crushable instanceOf Dream) {
...
}
if (curshable instanceOf Garlic) {
...
}
But casting from Garlic to Dream is not an option given that one is not a subtype of the other.
Simply use method overloading.
public void utterlyDestroy(Dreams parameter) {
parameter.crush();
}
public void utterlyDestroy(Garlic parameter) {
parameter.crush();
}
If you want to support more than these two types in the same way, you can define a common interface for them all and use generics.