ifelse是任何编程语言的重要组成部分。可是咱们编写了大量嵌套的if语句,这使得咱们的代码更加复杂和难以维护。express
接下来,让咱们探索如何简化代码的中的ifelse语句写法。编程
咱们常常遇到涉及不少条件的业务逻辑,而且每一个逻辑都须要不一样的处理方式。以Calculator类为例。咱们将有一个方法,它接受两个数字和一个运算符做为输入,并根据操做返回结果:设计模式
public int calculate(int a, int b, String operator) {
int result = Integer.MIN_VALUE;
if ("add".equals(operator)) {
result = a + b;
} else if ("multiply".equals(operator)) {
result = a * b;
} else if ("divide".equals(operator)) {
result = a / b;
} else if ("subtract".equals(operator)) {
result = a - b;
}
return result;
}
复制代码
咱们也可使用switch语句来实现它:bash
public int calculateUsingSwitch(int a, int b, String operator) {
switch (operator) {
case "add":
result = a + b;
break;
// other cases
}
return result;
}
复制代码
在典型的开发中,if语句可能会变得更大,更复杂。此外,当存在复杂条件时,switch语句不适合。app
拥有嵌套决策结构的另外一个反作用是它们变得难以管理。例如,若是咱们须要添加一个新的运算符,咱们必须添加一个新的if语句并实现该操做。编程语言
能够经过设计模式,来达到咱们要的效果。ide
不少时候,咱们遇到ifelse结构,最终在每一个分支中执行相似的操做。这提供了提取工厂方法的机会,该工厂方法返回给定类型的对象并基于具体对象行为执行操做。ui
对于咱们的示例,让咱们定义一个具备单个apply方法的Operation接口:this
public interface Operation {
int apply(int a, int b);
}
复制代码
该方法将两个数字做为输入并返回结果。让咱们定义一个用于执行添加的类:lua
public class Addition implements Operation {
@Override
public int apply(int a, int b) {
return a + b;
}
}
复制代码
咱们如今将实现一个工厂类,它根据给定的运算符返回Operation的实例:
public class OperatorFactory {
static Map<String, Operation> operationMap = new HashMap<>();
static {
operationMap.put("add", new Addition());
operationMap.put("divide", new Division());
// more operators
}
public static Optional<Operation> getOperation(String operator) {
return Optional.ofNullable(operationMap.get(operator));
}
}
复制代码
如今,在Calculator类中,咱们能够查询工厂以获取相关操做并应用源数:
public int calculateUsingFactory(int a, int b, String operator) {
Operation targetOperation = OperatorFactory
.getOperation(operator)
.orElseThrow(() -> new IllegalArgumentException("Invalid Operator"));
return targetOperation.apply(a, b);
}
复制代码
在这个例子中,咱们已经看到了如何将责任委托给工厂类提供的松散耦合对象。可是有可能嵌套的if语句只是转移到了工厂类,这违背了咱们的目的。
或者,咱们能够在Map中维护一个对象存储库,能够查询该存储库以进行快速查找。正如咱们所见,OperatorFactory#operationMap服务于咱们的目的。咱们还能够在运行时初始化Map并将它们配置为查找。
除了使用Map以外,咱们还可使用Enum来标记特定的业务逻辑。以后,咱们能够在嵌套的if语句或switch case 语句中使用它们。或者,咱们也能够将它们用做对象的工厂并制定策略以执行相关的业务逻辑。
这样能够减小嵌套if语句的数量,并将责任委托给单个Enum值。
让咱们看看咱们如何实现它。首先,咱们须要定义咱们的枚举:
public enum Operator {
ADD, MULTIPLY, SUBTRACT, DIVIDE
}
复制代码
能够观察到,这些值是不一样运算符的标签,将进一步用于计算。咱们老是能够选择在嵌套的if语句或switch case中使用这些值做为不一样的条件,但让咱们设计一种将逻辑委托给Enum自己的替代方法。
咱们将为每一个Enum值定义方法并进行计算。例如:
ADD {
@Override
public int apply(int a, int b) {
return a + b;
}
},
// other operators
public abstract int apply(int a, int b);
复制代码
而后在Calculator类中,咱们能够定义一个执行操做的方法:
public int calculate(int a, int b, Operator operator) { return operator.apply(a, b); }
如今,咱们能够经过使用Operator#valueOf()
方法将String值转换为Operator来调用该方法:
@Test
public void test() {
Calculator calculator = new Calculator();
int result = calculator.calculate(3, 4, Operator.valueOf("ADD"));
assertEquals(7, result);
}
复制代码
在前面的讨论中,咱们已经看到使用工厂类来返回给定运算符的正确业务对象的实例。稍后,业务对象用于在计算器中执行计算。
咱们还能够设计一个Calculator#calculate方法来接受能够在输入上执行的命令。这将是替换嵌套if语句的另外一种方法。
咱们首先定义咱们的Command接口:
public interface Command {
Integer execute();
}
复制代码
接下来,让咱们实现一个AddCommand:
public class AddCommand implements Command {
// Instance variables
public AddCommand(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public Integer execute() {
return a + b;
}
}
复制代码
最后,让咱们在Calculator中引入一个接受并执行Command的新方法:
public int calculate(Command command) {
return command.execute();
}
复制代码
接下来,咱们能够经过实例化AddCommand调用计算并将其发送到Calculator#calculate方法:
@Test
public void test() {
Calculator calculator = new Calculator();
int result = calculator.calculate(new AddCommand(3, 7));
assertEquals(10, result);
}
复制代码
当咱们最终编写大量嵌套if语句时,每一个条件都描述了一个业务规则,必须对其进行评估才能处理正确的逻辑。规则引擎从主代码中获取了这种复杂性。一个RuleEngine
评估规则和返回基于输入的结果。
让咱们经过设计一个简单的RuleEngine来演示一个例子,该RuleEngine经过一组规则处理Expression并返回所选规则的结果。首先,咱们将定义一个Rule接口:
public interface Rule {
boolean evaluate(Expression expression);
Result getResult();
}
复制代码
其次,让咱们实现一个RuleEngine:
public class RuleEngine {
private static List<Rule> rules = new ArrayList<>();
static {
rules.add(new AddRule());
}
public Result process(Expression expression) {
Rule rule = rules
.stream()
.filter(r -> r.evaluate(expression))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Expression does not matches any Rule"));
return rule.getResult();
}
}
复制代码
所述RuleEngine接受一个表达对象,并返回结果。如今,让咱们将Expression类设计为一组包含两个Integer对象的Operator,它将被应用:
public class Expression {
private Integer x;
private Integer y;
private Operator operator;
}
复制代码
最后让咱们定义一个自定义的AddRule类,该类仅在指定ADD操做时进行求值:
public class AddRule implements Rule {
@Override
public boolean evaluate(Expression expression) {
boolean evalResult = false;
if (expression.getOperator() == Operator.ADD) {
this.result = expression.getX() + expression.getY();
evalResult = true;
}
return evalResult;
}
}
复制代码
咱们如今将使用Expression调用RuleEngine:
@Test
public void test() {
Expression expression = new Expression(5, 5, Operator.ADD);
RuleEngine engine = new RuleEngine();
Result result = engine.process(expression);
assertNotNull(result);
assertEquals(10, result.getValue());
}
复制代码
经过这些设计模式,能够做为咱们的ifelse语句的替代方案,具体用哪种能够根据你的实际业务场景来决定。