过多if-else分支的优化
2017-11-22 10:43
399 查看
JAVA程序经常看到一个函数中包含了十几个if-else的分支判断
比如 根据不同职位增加工资的例子
public class Employee {
private int salary = 10;
public void
addMoney(String str) {
if ("初级JAVA程序员".equals(str)) {
this.salary = salary + 30;
} else if ("中级JAVA程序员".equals(str)) {
this.salary = salary + 50;
} else if ("高级JAVA程序员".equals(str)) {
this.salary = salary + 80;
} else if ("架构师".equals(str)) {
this.salary = salary + 100;
} else if ("数据库管理员".equals(str)) {
this.salary = salary + 100;
} else if ("项目经理".equals(str)) {
this.salary = salary + 100;
} else if ("菜鸟".equals(str)) {
this.salary = salary + 10;
}
// ...
System.out.println(this.salary);
}
public static
void main(String[] args) {
Employee e = new
Employee();
e.addMoney("菜鸟");
}
}
可以改写如下
import java.util.HashMap;
import java.util.Map;
public class Employee {
private int salary = 10;
private interface IStrategy {
int addMoney();
};
Map<String, IStrategy> strategy = new
HashMap<String, IStrategy>();
{
strategy.put("初级JAVA程序员", new
IStrategy() {
@Override
public int addMoney() {
return 30;
}
});
strategy.put("中级JAVA程序员", new
IStrategy() {
@Override
public int addMoney() {
return 50;
}
});
strategy.put("高级JAVA程序员", new
IStrategy() {
@Override
public int addMoney() {
return 80;
}
});
strategy.put("架构师", new
IStrategy() {
@Override
public int addMoney() {
return 100;
}
});
strategy.put("数据库管理员", new
IStrategy() {
@Override
public int addMoney() {
return 100;
}
});
strategy.put("项目经理", new
IStrategy() {
@Override
public int addMoney() {
return 100;
}
});
strategy.put("菜鸟", new
IStrategy() {
@Override
public int addMoney() {
return 10;
}
});
}
public void addMoney(String str) {
this.salary = salary + strategy.get(str).addMoney();
System.out.println(this.salary);
}
public static void main(String[] args) {
Employee e = new Employee();
e.addMoney("菜鸟");
}
}
如果是复杂的项目,可以把匿名内部类改为普通类.
改写之后,无论新增或者删除逻辑,都非常的简单明了,并且逻辑的变更,限制在了一个较小的范围.
实现了高内聚,低耦合的设计思路.
比如 根据不同职位增加工资的例子
public class Employee {
private int salary = 10;
public void
addMoney(String str) {
if ("初级JAVA程序员".equals(str)) {
this.salary = salary + 30;
} else if ("中级JAVA程序员".equals(str)) {
this.salary = salary + 50;
} else if ("高级JAVA程序员".equals(str)) {
this.salary = salary + 80;
} else if ("架构师".equals(str)) {
this.salary = salary + 100;
} else if ("数据库管理员".equals(str)) {
this.salary = salary + 100;
} else if ("项目经理".equals(str)) {
this.salary = salary + 100;
} else if ("菜鸟".equals(str)) {
this.salary = salary + 10;
}
// ...
System.out.println(this.salary);
}
public static
void main(String[] args) {
Employee e = new
Employee();
e.addMoney("菜鸟");
}
}
可以改写如下
import java.util.HashMap;
import java.util.Map;
public class Employee {
private int salary = 10;
private interface IStrategy {
int addMoney();
};
Map<String, IStrategy> strategy = new
HashMap<String, IStrategy>();
{
strategy.put("初级JAVA程序员", new
IStrategy() {
@Override
public int addMoney() {
return 30;
}
});
strategy.put("中级JAVA程序员", new
IStrategy() {
@Override
public int addMoney() {
return 50;
}
});
strategy.put("高级JAVA程序员", new
IStrategy() {
@Override
public int addMoney() {
return 80;
}
});
strategy.put("架构师", new
IStrategy() {
@Override
public int addMoney() {
return 100;
}
});
strategy.put("数据库管理员", new
IStrategy() {
@Override
public int addMoney() {
return 100;
}
});
strategy.put("项目经理", new
IStrategy() {
@Override
public int addMoney() {
return 100;
}
});
strategy.put("菜鸟", new
IStrategy() {
@Override
public int addMoney() {
return 10;
}
});
}
public void addMoney(String str) {
this.salary = salary + strategy.get(str).addMoney();
System.out.println(this.salary);
}
public static void main(String[] args) {
Employee e = new Employee();
e.addMoney("菜鸟");
}
}
如果是复杂的项目,可以把匿名内部类改为普通类.
改写之后,无论新增或者删除逻辑,都非常的简单明了,并且逻辑的变更,限制在了一个较小的范围.
实现了高内聚,低耦合的设计思路.
相关文章推荐
- java中过多if-else分支语句的优化方案
- 过多if-else分支的优化
- 过多if-else分支的优化
- 过多if-else分支的优化
- 过多if-else分支的优化
- 过多if-else分支的优化
- CPU与代码优化(1):用三元操作符替代if-else以降低CPU分支预测惩罚;函数13倍提速(Unity)。
- java中 if-else分支语句的优化方案
- 过长的if-else分支结构优化
- 使用case语句来改善verilog代码中if……else分支过多的问题
- Android设计模式 -- 巧用策略模式告别过多的 if...else...
- C语言基础之分支结构—if...else—elseif—switch...case
- if-else的优化举例
- 应用分支结构if...else....
- [iOS]C语言技术视频-02-程序分支结构(if...else)
- switch...case 和 if...else效率比较和优化
- 10、C#里面的分支结构if和else
- 实验三——for 语句及分支结构else-if
- Java 分支结构 - if...else/switch
- if---else---if分支的执行次序。