java if语句优化设计模式_如何优化代码中的多重if

原文:https://www.baeldung.com/java-replace-if-statements

1.利用工厂模式

/**

* 通过工厂模式来代替if

* */

public class RelIfByFactory {

public static void main(String[] args) {

System.out.println(calculate(3,6));

}

private static int calculate(int a, int b) {

// TODO Auto-generated method stub

Operation operation = OperatorFactory.getOperation("add")

.orElseThrow(()->new RuntimeException("参数非法"));

return operation.apply(a, b);

}

}

interface Operation{

int apply(int a,int b);

}

class Addition implements Operation{

@Override

public int apply(int a, int b) {

// TODO Auto-generated method stub

return a+b;

}

}

class OperatorFactory{

static Map operMap = new HashMap();

static {

operMap.put("add", new Addition());

}

static Optional getOperation(String operator){

return Optional.ofNullable(operMap.get(operator));

}

}

2.利用枚举类

package releaseif;

/**

* 通过枚举类来代替多重if

*/

public class RelIfByEnums {

public static void main(String[] args) {

System.out.println(calculate(9, 3, Operator.valueOf("DIVIDE")));

}

private static int calculate(int a, int b, Operator operator) {

// TODO Auto-generated method stub

return operator.apply(a, b);

}

}

enum Operator {

ADD {

@Override

public int apply(int a, int b) {

return a + b;

}

},

MULTIPLY {

@Override

public int apply(int a, int b) {

return a * b;

}

},

SUBTRACT {

@Override

public int apply(int a, int b) {

return a - b;

}

},

DIVIDE {

@Override

public int apply(int a, int b) {

return a / b;

}

};

public abstract int apply(int a, int b);

}

3.通过命令模式

package releaseif;

/**

* 通过命令模式代替多重if

*/

public class RelIfByCommand {

public static void main(String[] args) {

System.out.println(calculate(new AddCommand(5, 8)));

}

private static int calculate(AddCommand command) {

// TODO Auto-generated method stub

return command.execute();

}

}

interface Command {

int execute();

}

class AddCommand implements Command {

int a;

int b;

public AddCommand(int a, int b) {

super();

this.a = a;

this.b = b;

}

@Override

public int execute() {

// TODO Auto-generated method stub

return a + b;

}

}

4.利用规则引擎

package releaseif;

import java.util.ArrayList;

import java.util.List;

/**

* 通过规则引擎代替多重if

*/

public class RelIfByRuleEngine {

public static void main(String[] args) {

Expression expression = new Expression(5, 7, Operator.ADD);

System.out.println(new RuleEngine().process(expression).getValue());

}

}

class Result {

int value;

public Result(int value) {

super();

this.value = value;

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

}

class RuleEngine {

static List rules = new ArrayList();

static {

rules.add(new AddRule());

}

Result process(Expression expression) {

Rule rule = rules.stream().filter(r -> r.evaluate(expression)).findFirst()

.orElseThrow(() -> new RuntimeException("error"));

return rule.getResult();

}

}

interface Rule {

boolean evaluate(Expression expression);

Result getResult();

}

class Expression {

private int x;

private int y;

private Operator operator;

public Expression(int i, int j, Operator operator) {

// TODO Auto-generated constructor stub

this.x = i;

this.y = j;

this.operator = operator;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

public Operator getOperator() {

return operator;

}

public void setOperator(Operator operator) {

this.operator = operator;

}

}

class AddRule implements Rule {

int result;

@Override

public boolean evaluate(Expression expression) {

// TODO Auto-generated method stub

var evalResult = false;

if (expression.getOperator() == Operator.ADD) {

result = expression.getX() + expression.getY();

evalResult = true;

}

return evalResult;

}

@Override

public Result getResult() {

// TODO Auto-generated method stub

return new Result(result);

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值