过多if-else分支的优化

JAVA程序经常看到一个函数中包含了十几个if-else的分支判断
比如  根据不同职位增加工资的 例子

  1. public class Employee {
  2.     private int salary = 10;

  3.     public void addMoney(String str) {
  4.         if ("初级JAVA程序员".equals(str)) {
  5.             this.salary = salary + 30;
  6.         } else if ("中级JAVA程序员".equals(str)) {
  7.             this.salary = salary + 50;
  8.         } else if ("高级JAVA程序员".equals(str)) {
  9.             this.salary = salary + 80;
  10.         } else if ("架构师".equals(str)) {
  11.             this.salary = salary + 100;
  12.         } else if ("数据库管理员".equals(str)) {
  13.             this.salary = salary + 100;
  14.         } else if ("项目经理".equals(str)) {
  15.             this.salary = salary + 100;
  16.         } else if ("菜鸟".equals(str)) {
  17.             this.salary = salary + 10;
  18.         }
  19.         // ...
  20.         System.out.println(this.salary);
  21.     }

  22.     public static void main(String[] args) {
  23.         Employee e = new Employee();
  24.         e.addMoney("菜鸟");
  25.     }
  26. }
可以改写如下

  1. import java.util.HashMap;
  2. import java.util.Map;

  3. public class Employee {
  4.     private int salary = 10;

  5.     private interface IStrategy {
  6.         int addMoney();
  7.     };

  8.     Map<String, IStrategy> strategy = new HashMap<String, IStrategy>();

  9.     {
  10.         strategy.put("初级JAVA程序员", new IStrategy() {
  11.             @Override
  12.             public int addMoney() {
  13.                 return 30;
  14.             }
  15.         });

  16.         strategy.put("中级JAVA程序员", new IStrategy() {
  17.             @Override
  18.             public int addMoney() {
  19.                 return 50;
  20.             }
  21.         });

  22.         strategy.put("高级JAVA程序员", new IStrategy() {
  23.             @Override
  24.             public int addMoney() {
  25.                 return 80;
  26.             }
  27.         });

  28.         strategy.put("架构师", new IStrategy() {
  29.             @Override
  30.             public int addMoney() {
  31.                 return 100;
  32.             }
  33.         });

  34.         strategy.put("数据库管理员", new IStrategy() {
  35.             @Override
  36.             public int addMoney() {
  37.                 return 100;
  38.             }
  39.         });

  40.         strategy.put("项目经理", new IStrategy() {
  41.             @Override
  42.             public int addMoney() {
  43.                 return 100;
  44.             }
  45.         });

  46.         strategy.put("菜鸟", new IStrategy() {
  47.             @Override
  48.             public int addMoney() {
  49.                 return 10;
  50.             }
  51.         });

  52.     }

  53.     public void addMoney(String str) {
  54.         this.salary = salary + strategy.get(str).addMoney();
  55.         System.out.println(this.salary);
  56.     }

  57.     public static void main(String[] args) {
  58.         Employee e = new Employee();
  59.         e.addMoney("菜鸟");
  60.     }
  61. }
如果是复杂的项目,可以把匿名内部类改为普通类.

改写之后,无论新增或者删除逻辑,都非常的简单明了,并且逻辑的变更,限制在了一个较小的范围.
实现了高内聚,低耦合的设计思路.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值