ITPub博客

首页 > 应用开发 > Java > 过多if-else分支的优化

过多if-else分支的优化

原创 Java 作者:壹頁書 时间:2015-01-21 17:34:25 1 删除 编辑
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. }
如果是复杂的项目,可以把匿名内部类改为普通类.

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

参考:
http://raychase.iteye.com/blog/1814187

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1409748/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6343406