Object Oriented Language has very powerful feature of Polymorphism, it is used to remove if/else or switch case in code.
Code without condition is easy to read. There are some places where you have to put them and one of such example is Factory/ServiceProvider class.
I am sure you have seen factory class with IF-ELSEIF which keeps on getting big.
In this blog i will share some techniques that you can be used to remove condition in factory class.
I will use below code snippet as example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Validator newInstance(String validatorType) { | |
if ("INT".equals(validatorType)) | |
return new IntValidator(); | |
else if ("DATE".equals(validatorType)) | |
return new DateValidator(); | |
else if ("LOOKUPVALUE".equals(validatorType)) | |
return new LookupValueValidator(); | |
else if ("STRINGPATTERN".equals(validatorType)) | |
return new StringPatternValidator(); | |
return null; | |
} |
Reflection
This is first thing that comes to mind when you want to remove conditions. You get the feeling of framework developer!
This looks very simple but only problem is caller has to remember fully qualified class name and some time it could be issue.
Map
Map can be used to to map actual class instance to some user friendly name
This also looks neat without overhead of reflection.
Enum
This is interesting one
This method is using enum method to remove condition, one of the issue is that you need Enum for each type. You don't want to create tons of them!
I personally like this method.
Conclusion
If-else or switch case makes code difficult to understand, we should try to avoid them as much as possible. Language construct should be used to avoid some of switch case.
We should try to code without IF-ELSE and that will force us to come up with better solution.
This is first thing that comes to mind when you want to remove conditions. You get the feeling of framework developer!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Validator newInstance(String validatorClass) { | |
return Class.forName(validatorClass).newInstance(); | |
} |
This looks very simple but only problem is caller has to remember fully qualified class name and some time it could be issue.
Map
Map can be used to to map actual class instance to some user friendly name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String, Validator> validators = new HashMap<String,Validator>(){ | |
{ | |
put("INT",new IntValidator()); | |
put("LOOKUPVALUE",new LookupValueValidator()); | |
put("DATE",new DateValidator()); | |
put("STRINGPATTERN",new StringPatternValidator()); | |
} | |
}; | |
public Validator newInstance(String validatorType) { | |
return validators.get(validatorType); | |
} |
Enum
This is interesting one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum ValidatorType { | |
INT { | |
public Validator create() { | |
return new IntValidator(); | |
} | |
}, | |
LOOKUPVALUE { | |
public Validator create() { | |
return new LookupValueValidator(); | |
} | |
}, | |
DATE { | |
public Validator create() { | |
return new DateValidator(); | |
} | |
}; | |
public Validator create() { | |
return null; | |
} | |
} | |
public Validator newInstance(ValidatorType validatorType) { | |
return validatorType.create(); | |
} |
I personally like this method.
Conclusion
If-else or switch case makes code difficult to understand, we should try to avoid them as much as possible. Language construct should be used to avoid some of switch case.
We should try to code without IF-ELSE and that will force us to come up with better solution.