What is the key in your map? if the key is the Remote.type
then you can just do
rootClass = map.get(remote.type).translate(remote);
Which does get rid of the if/else if blocks. Just be sure to handle unknown/ untranslated regions or have a NullObject
that doesn't translate or performs a default translation.
The technical name for this from the book Refactoring to Patterns is called "Replace Conditional Dispatcher with Command"
You still have to populate the map though. Perhaps one way to do this is to make all the RootAdoper
interface an enum and all implementations the types in the enum. You can also add a new method to the enum to get the Remote.Type that each value can translate.
enum RootAdoper{
A{
@Overide
public RootClass translate(RemoteClass remote){
//...
}
@Override
public String getTypeToTranslate(){
return "A";
}
},
... // other types listed here similarly
;
abstract RootClass translate(RemoteClass remote);
abstract String getTypeToTranslate();
}
Then you can populate the Map like this
Map<String, RootAdoper> map = new HashMap<String, RootAdoper>();
for(RootAdoper adoper : RootAdoper.values(){
map.put(adoper.getTypeToTranslate(), adoper);
}