【问题标题】:How to define Whitelist approach on Java to be able to return specific 'Type' of whitelist?如何在 Java 上定义白名单方法以能够返回白名单的特定“类型”?
【发布时间】:2021-04-16 06:42:57
【问题描述】:

伙计们。我现在正面临一个难以决定的情况(在我的脑海中),当我设计我的算法来过滤和检查某个事物时,比如 CallCenter,有一个针对某个电话号码或某个区域的白名单并返回特定类型的白名单CallCenter 应用在​​他们身上。是电话号码白名单,还是某个区域,甚至是区域和电话的组合(特定目标)。

顺便说一句,在我的设计中,呼叫中心将只有一种类型的白名单(“byPhone”、“byArea”或“byAreaAndPhone”)我的方法是这样的:

//// 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

然后,在我的脑海中,我已经觉得当开发变得更大时会很奇怪,比如说呼叫中心白名单将有更多类型的白名单,那么最后一个方法执行的 if 也会被缩放,并且循环内的 if 语句也会缩放。任何人都可以帮助我决定在我制作它时是否足够好,或者我应该将验证分开以成为每种类型白名单上的一对一验证?起初我决定不分离函数,因为 CallCenter 只会包含一种类型的白名单,但无论白名单中的类型条件如何,错误返回都会始终检查类型。

**注意:顺便说一句,我已经为这种情况设计了一个黑名单,黑名单有相反的方法,可以更容易地应用错误规范,因为每当在循环中发现的东西都会抛出错误,这意味着错误类型是与我发现的与规范相同的确切元素相同(如 byPhone 或 byArea)。

【问题讨论】:

  • 仅供参考,您可能想更改您在此处使用的条款,白名单和黑名单正在从许多主要公司代码库中删除。
  • @CheeseFerret 哦,我没有更新当前的内容,为什么现在要删除它们?你能帮我找到这里的方向吗
  • 当然,这里有一些最近的链接techspot.com/news/…beebom.com/google-blacklist-whitelist-removed-code。我希望这些对您有所帮助。

标签: java spring performance architecture clean-architecture


【解决方案1】:

这种类型的东西完全可以通过将“可列入白名单的东西”和“白名单类型”封装到各自的类中来处理。

创建一个表示“要针对的事物”的接口意味着您可以将这一轮传递给所有实现。只要它可以访问所有那种类型需要的东西,它就不需要关心你以后是否添加更多细节:

public interface WhitelistContext {
    AreaName getAreaName();
    PhoneNumber getPhoneNumber();
}

然后您可以让您的WhitelistType 成为另一个 接口,该接口接受其中一个WhitelistContexts。

public interface WhitelistType {
    public void validate(WhitelistContext context) throws ValidationException;
}

然后让每个WhitelistType 类负责自己的验证:

public class AreaWhitelistType implements WhitelistType {
    @Override
    public void validate(final WhitelistContext context) {
        AreaName area = context.getAreaName();
        // Do whatever else you need here...
    }
}

这样,如果您向 WhitelistContext 添加更多元素,它不会影响任何现有的 WhitelistTypes,您不需要为您的方法提供不断增长的参数列表,而不是让一个覆盖所有类型的 if (...) else 块,您可以循环遍历,将 WhitelistContext 传递给每个类型并让 it 担心。

为了处理不同的CallCenters,您可以将它作为参数添加到WhitelistType 接口,以便它可以从那里获取详细信息,或者让该类的实现的构造函数取一个并知道如何使用它。无论哪种方式都可以恕我直言。

【讨论】:

  • hmm,很有趣,但我希望WhitelistType 是一个枚举,我在上面使用字符串,因为我不知道枚举是否是正确答案。我也使用枚举能够通过 JPA 转换为存储在数据库中的字符串
  • enums 可以实现 interfaces 所以没有什么可以阻止你。尽管这确实会阻止您在构造函数中传递 CallCenter,但这意味着它必须是 validate 方法的另一个参数。
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多