是否有人不同意以下说法:“使用开关是糟糕的OOP风格"? [英] Does anyone disagree with the statement: "using switch is bad OOP style"?

查看:66
本文介绍了是否有人不同意以下说法:“使用开关是糟糕的OOP风格"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到它在stackoverflow上用多个线程/注释编写,使用switch只是糟糕的OOP风格.我个人不同意这一点.

I have seen it written in multiple threads/comments on stackoverflow that using switch is just bad OOP style. Personally I disagree with this.

在很多情况下,由于无法控制它们而无法将代码(即方法)添加到要打开的enum类中,这可能是因为它们在第三方jar文件中.在其他情况下,将功能放在枚举本身上是一个坏主意,因为它违反了一些关注点分离的注意事项,或者实际上是以及其他功能的功能枚举.

There will be many cases where you cannot add code (i.e. methods) to enum classes you want to switch on because you don't control them, perhaps they are in a 3rd-party jar file. There will be other cases where putting the functionality on the enum itself is a bad idea because it violates some separation-of-concerns considerations, or it is actually a function of something else as well as the enumeration.

最后,开关简洁明了:

boolean investable;
switch (customer.getCategory()) {
    case SUB_PRIME:
    case MID_PRIME:
        investible = customer.getSavingsAccount().getBalance() > 1e6; break;
    case PRIME:
        investible = customer.isCeo(); break;
}

我并不是为switch的每一种用法辩护,也不是说这总是要走的路.但是在我看来,"Switch是一种代码味道"这样的说法是错误的.还有其他人同意吗?

I'm not defending every use of switch and I'm not saying it's always the way to go. But statements like "Switch is a code smell" are just wrong, in my opinion. Does anyone else agree?

推荐答案

进行后续跟踪:

如果这仅仅是希望获得商业贷款的客户的可投资性"逻辑,该怎么办?也许客户对另一种产品的可投资性决策确实有很大不同……而且,如果一直有新产品问世,每种产品具有不同的可投资性决策,而我又不想每次更新我的核心客户类,该怎么办?什么时候发生?

和您的评论之一:

我不太确定是否将逻辑保持在其所操作的数据附近.现实世界不是这样工作的.当我请求贷款时,银行会决定我是否符合条件.他们不要求我自己决定.

就此,你是对的.

boolean investable = customer.isInvestable();

不是您所谈论的灵活性的最佳解决方案.但是,最初的问题没有提到存在单独的Product基类.

is not the optimal solution for the flexibility you're talking about. However, the original question didn't mention the existence of a separate Product base class.

鉴于现在可获得的其他信息,最好的解决方案似乎是

Given the additional information now available, the best solution would appear to be

boolean investable = product.isInvestable(customer);

产品根据您的现实世界"论点(多态性!)做出可投资性决定,并且还避免了每次添加产品时都必须创建新的客户子类的情况.产品可以根据客户的公共界面使用其想要确定的任何方法.我仍然会质疑是否可以对客户界面进行适当的添加以消除切换的需要,但这仍然是所有弊病中最少的.

The investability decision is made (polymorphically!) by the Product, in accordance with your "real world" argument and it also avoids having to make new customer subclasses each time you add a product. The Product can use whatever methods it wants to make that determination based on the Customer's public interface. I'd still question whether there are appropriate additions which could be made to Customer's interface to eliminate the need for switch, but it may still be the least of all evils.

不过,在提供的特定示例中,我很想做类似的事情:

In the particular example provided, though, I'd be tempted to do something like:

if (customer.getCategory() < PRIME) {
    investable = customer.getSavingsAccount().getBalance() > 1e6;
} else {
    investable = customer.isCeo();
}

我发现此方法比列出交换机中的每个可能类别更清晰,更清晰,我怀疑它更可能反映现实世界"的思维过程(它们低于主要条件吗?"与它们是次要条件还是次要条件?中期"?),并且如果在某个时候添加了SUPER_PRIME名称,则可以避免重新访问此代码.

I find this cleaner and clearer than listing off every possible category in a switch, I suspect it's more likely to reflect the "real world" thought processes ("are they below prime?" vs. "are they sub-prime or mid-prime?"), and it avoids having to revisit this code if a SUPER_PRIME designation is added at some point.

这篇关于是否有人不同意以下说法:“使用开关是糟糕的OOP风格"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆