Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

spring - How to define Whitelist approach on Java to be able to return specific 'Type' of whitelist?

guys. I am facing a hard to decide situation(in my head)right now when I design my algorithm to filter and check whenever a things, let say CallCenter, has a whitelist for some phone number or some area and return specific type of whitelisting that the CallCenter applied on them. is it whitelist for phonenumber, or for some area, or even the combination of area and phone(specific target).

Btw, in my design a CallCenter will only has one type of Whitelist ("byPhone", "byArea", or "byAreaAndPhone" )I have my approach like this:

//// I use call center one to many relationship to whitelist directly to get the whitelists
private void validateWhitelistOfCallCenter(CallCenter callCenter, String phoneNumber, String areaName) {
    if (callCenter.getWhitelists().isEmpty()) return;
    String typeWhitelist
    for (CallCenterWhitelist whitelist : callCenter.getWhitelists()) {
        typeWhitelist = whitelist.getType();
        if (WHITELIST_BY_PHONE_NUMBER.equals(whitelist.getType())
                && whitelist.getCustomerPhoneNumber().equals(phoneNumber)) return;

        if (WHITELIST_BY_AREA.equals(whitelist.getType())
                && whitelist.getAreaName().equals(areaName)) return;

        if (WHITELIST_BY_PHONE_NUMBER_AND_AREA.equals(whitelist.getType())
                && whitelist.getAreaName().equals(areaName)
                && whitelist.getCustomerPhoneNumber().equals(phoneNumber)) return;
    }
    if (WHITELIST_BY_PHONE_NUMBER.equals(errorType))
        throw CallCenterErrors.customerPhoneIsNotInWhitelist(phoneNumber);
    if (WHITELIST_BY_AREA.equals(errorType),
        throw CallCenterErrors.customerAreaIsNotInWhitelist(areaName);
    if (WHITELIST_BY_PHONE_NUMBER_AND_AREA.equals(errorType),
        throw CallCenterErrors.customerAreaAndPhoneIsNotInWhitelist(phoneNumber, areaName);
}
/// note: each return means the customer is in whitelist or the callcenter doesn't use anywhitelist kind of things

Then, in my head I already feels it will be weird when the development becomes bigger let say callcenter whitelist will has more type of whitelist, then the if on the last method execution will be scaling too, and if statement inside the loop will be scaling too. Can anyone help me to decide is it good enough to do it as I make it or should I separate the validation to become one on one validation on each type of whitelist?. At first I decide not to separate the function because CallCenter Will only contain one type of whitelist, but the error return will always check the type whatever the condition of type in whitelists.

**Notes: Btw I already design a Blacklist for this kind of situation, blacklist has the opposite apporoach that makes it easier to apply error specification because whenever things found on loop throw error, which means the error type is same as the exact element that I found same with the specification(like byPhone or byArea).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 WhitelistContexts.

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 WhitelistTypes, 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 CallCenters, 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...