为什么80%的码农都做不了架构师?>>>
原文: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);
}
}