Instead of using if/else I'd like to use a map to get a concrete implementation.
I'd like to use generics to declare a specific entity handler witch handles a specific entity but I have the problem that the handler just accepts extensions of my entity interface. The code looks like this:
private Map<Class<IEntity>, IEntityHandler> handlers;
public void callingMethod(IModel model) {
for (IEntity entity : model.getObjects()) {
// handle accepts just IDevice and not IEntity !
handlers.get(entity).handle(entity);
}
}
public interface IEntityHandler<T extends IEntity> {
handle(T entity);
}
public class DeviceHandler implements IEntityHandler<IDevice> {
@Override
public void handle(IDevice deviceEntity) {
// do something
}
}
How can I get my handle method that it takes IEntity's? IDevice extends from IEntity.