c# - 重构ifs

标签 c# refactoring

如何重构以下包含许多 if 且每个 block 包含不同场景的代码。

if(condition1)
{
  ConfigCondition1();
  GetSetCondition1Results();
}
if(condition2)
{
  ConfigCondition2();
  GetSetCondition2Results();
}
if(condition3)
{
  ConfigCondition3();
  GetSetCondition3Results();
}

最佳答案

通过使用 Strategy Design Pattern .

策略模式将算法封装在对象中,帮助您将条件语句重构为简单的函数调用。

Here is a nice example in .NET你可以使用。

关于c# - 重构ifs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8654294/

相关文章:

refactoring - 你用残暴的代码做什么?

c# - 两个值之间的正则表达式返回值?

design-patterns - 重构 switch 语句时何时选择策略而不是多态

C 代码 reshape 器

c# - 如何以编程方式创建新的 Mongo 数据库?

c# - 有什么工具可以帮助我将方法调用从使用基于位置的参数重构为基于名称的参数

ios - Objective-C : How to check object type without a lot of if statements

c# - 为什么我使用库 A 的 C# 客户端需要有库 B(A 使用)的 using 语句

c# - 如何创建临时文件来缓存库的启动工作?

c# - 如何使用 C# 将词汇表解析为数据库?