- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个值,只有在满足某些条件时才需要从方法返回,否则我必须返回 null。
该方法正在获取 2 个 boolean 参数,根据这些参数调用进一步的方法来检查其他一些条件。
Call method1, then method2 -> if (param1 && param2) == true;
Don't call any method-> if (!param1 && !param2) == true;
Call only method1 -> if only param1 == true;
Call only method2 -> if only param2 == true; //Method2 is an expensive operation.
所以,为了满足这个要求,我写了一段代码,如下所示 -
value = doSomeProcessingToGetValue(); //Not an expensive operation
if(param1 && param2) //both are true {
if(method1() != null) { // if method1 return true then move ahead
if(method2() != null) { //if method2 return true then move ahead (Expensive operation)
return value; //Means all the conditions are satisfied now
}
}
}
else if(!param1 && !param2) //both are false {
return value; //No need to check for any condition
}
else if(param1) //Means param1 is true {
if(method1()!=null) {// if method1 return true then move ahead
return value; //Means all the conditions are satisfied now
}
}
else { //Means param2 is true
if(method2()!=null) {// if method2 return true then move ahead(Expensive operation)
return value; //Means all the conditions are satisfied now
}
}
return null;
我对上述方法有 2 个顾虑 -
可扩展性 - 此解决方案不可扩展,因为将来可能需要检查更多参数及其各自要调用的方法。所以,没有。 if else 检查的数量将随着每个参数的增加呈指数增长。
不优雅 - 由于有太多的条件检查,它看起来不是很优雅。为了让它更优雅,我正在考虑应用Strategy pattern。
Strategy pattern approach 说到高层次,我将为给定的每个条件创建具体的类多于。因此,对于上面的代码,将有四个具体类。我将遍历所有具体类并检查它们是否满足条件并相应地返回值。
我的疑问是使用策略模式来满足这个要求是否有意义,因为这个解决方案在可伸缩性方面也表现不佳。
或者是否有任何更好的方法在可扩展性方面会有好处。
最佳答案
策略模式通常用于在运行时选择算法。所以,这可能是个不错的选择。
但是您写过“我将遍历所有具体类并检查它们是否满足条件”。您无需遍历实现。您可以使用例如 factory 在运行时创建策略实现:
public Strategy getStrategy(boolean param1, boolean param2) {
if (param1) {
if (param2) {
return value -> method1() != null && method2() != null ? value : null;
} else {
return value -> method1() != null ? value : null;
}
} else {
if (param2) {
return value -> method2() != null ? value : null;
} else {
return value -> value;
}
}
}
为了紧凑,我使用了 lambda,这只是示例。策略界面是:
interface Strategy {
Object getValue(Object value);
}
可以通过构造函数参数或设置方法将策略注入(inject)到您的主类中。如您所见,工厂成为您条件的另一个地方。因此,至少有一些条件进入了具体实现。
此外,replace condition with polymorphism重构模式和 stackoverflow discussion about it可能会有帮助。
还有一件事。看起来您的类实例的状态取决于 param1 和 param2。因此,其他设计解决方案可能是 finite state machine
关于java - 在这种情况下用策略模式替换复杂的条件语句是否有意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54732755/
我有一张 table People (First_Name, Last_Name)。此表包含与示例中一样重复的记录(并非所有行都重复): First_Name Last_Name John
我用 Java 编写过很多程序,之前也涉足过 C++。我在各种 C++ 书籍中阅读了有关指针的内容,并完成了书籍中的各种示例。 我了解指针的基础知识,但有一件事我一直不清楚。指针在现实世界中的应用是什
线 .Mappings(m => m.FluentMappings.AddFromAssemblyOf() 它有什么作用?它会在派生自 ClassMap 的 Product 类的程序集中查找任
我有用于打印数字的自定义打印功能。我制作了一个 ASCII 版本和一个 UTF-16LE 版本。 UTF-16LE 版本对 0-9 使用全角代码/字符,对十六进制使用 A-F。在调试我的函数时,我注意
这是我的代码片段: float ab(float); 以后 if(ab(temp)
我在一个项目文件中包含以下代码: //begin of the file ((window) => { 'use strict'; class View extends GSM.Event
我一直在到处寻找关于 ? 用法的正确解释。和 *。我注意到我可以使用以下方法拒绝所有用户的访问: 如果我想允许某个组,我应该在其上方添加下一行: 但是当我看到人们使用 ? 时,我开始忘记什么意思,
我正在关注 melon js tutorial .这是在我的 HUD.js 文件的顶部。 game.HUD = game.HUD || {} 我以前在其他例子中见过这个。 namespace.some
我正在处理一个包含数千行代码的文件。我正在第 700 行实现一个算法。我经常不得不离开这些行来检查文件中的其他方法。 导航回到我实际编码的地方通常很痛苦。如果我可以在第 700 行设置一个航路点并为其
我遇到了这段代码 do { if (higherQuality && w > targetWidth) { w /= 2; if (w &
uint8_t * const LCDMem = (uint8_t *) &LCDM3; 此代码在 msp430fg4618 培训套件中用于 lcd 配置。谁能解释一下上述代码的含义? 它允许使用 a
上下文 阅读一些内核代码。 问题 我不明白这行是什么意思 *(void **) &(int[2]){0,PAGE_SIZE}; 还有更多,这是什么意思 {0,PAGE_SIZE} 对我来说,它看起来不
我正在查看 Underscore.js 的源代码库,专门用于 map方法(该页面第 85 行左右,并复制到此处): _.map = function(obj, iterator, context)
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我是一名优秀的程序员,十分优秀!