This type of thing can be fully handled by encapsulating the 'things which can be whitelisted against', and the 'whitelist type' into their own classes.
Creating an interface which represents the 'things to whitelist against' means you can pass this round to all implementations. As long as it has access to all the things that that type needs it doesn't need to care if you add more details to it later:
public interface WhitelistContext {
AreaName getAreaName();
PhoneNumber getPhoneNumber();
}
You can then have your WhitelistType
be another interface which accepts one of these WhitelistContext
s.
public interface WhitelistType {
public void validate(WhitelistContext context) throws ValidationException;
}
Then have each WhitelistType
class be in charge of it's own validation:
public class AreaWhitelistType implements WhitelistType {
@Override
public void validate(final WhitelistContext context) {
AreaName area = context.getAreaName();
// Do whatever else you need here...
}
}
That way, if you add more elements to the WhitelistContext
it won't impact any of the existing WhitelistType
s, you don't need an ever-growing list of arguments to your method, and instead of having an if (...) else
block covering all the types, you can just loop through, passing the WhitelistContext
to each one and letting it worry about that.
For dealing with the different CallCenter
s, you can either add this to the WhitelistType
interface as an argument, so that it can get details from there, or have the constructor for the implementations of that class take one and know how to use it. Either way would be fine IMHO.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…