文章目录

一、使用策略模式 二、其他方案

text
1
<a href="#1_13">1.分解条件表达式</a>    <a href="#2_41">2.合并重复的条件判断</a>    <a href="#3_96">3.提前判断返回</a>    <a href="#4_113">4.引入断言工具类</a>    <a href="#5_Optional_130">5.善用 Optional</a>    <a href="#6_150">6.使用枚举</a>    <a href="#7_174">7.枚举多态</a>    <a href="#8_216">8.类多态</a>    <a href="#9_298">9.表驱动法</a>    <a href="#10_Map__Java8__340">10. Map + Java8 函数式接口</a>      

一、使用策略模式

彻底消灭if-else嵌套
设计模式-策略模式
如何干掉 Spring Boot 中大片的 if else?
业务代码中, if else 使用策略模式优化
设计模式 策略模式
极简策略模式 (妙用枚举)
责任链模式+策略模式

二、其他方案

1.分解条件表达式

text
1 2 3 4 5 6
if (date.before (SUMMER_START) || date.after(SUMMER_END)) {
   charge = quantity * _winterRate + _winterServiceCharge;
} else {
   charge = quantity * _summerRate
}

这种代码很多人可能都觉得没必要去提取方法,但是如果我们想要看懂这段代码,还是必须的去想想才知道在做什么;接下来我们修改一下

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
if (notSummer(date)) {
   charge = winterCharge(quantity);
} else {
   charge = summerCharge(quantity);
}

private boolean notSummer(Date date){
   date.before (SUMMER_START) || date.after(SUMMER_END)
}

private double summerCharge(int quantity) {
   return quantity * _summerRate;
}

private double winterCharge(int quantity) {
   return quantity * _winterRate + _winterServiceCharge;
}

2.合并重复的条件判断

text
1 2 3 4 5 6 7 8 9 10
double disabilityAmount () {
   if(_seniortiy <2 ) 
      return 0;
   if(_monthsDisabled > 12)
      return 0;
   if(_isPartTime)
      return 0;
   // 省略...
}

这里的条件返回的结果都是一样的,那么我们先把条件合并起来

text
1 2 3 4 5 6 7
double disabilityAmount () {
   if(_seniortiy <2 || _monthsDisabled > 12 || _isPartTime) {
      return 0;
   }
   // 省略...
}

接下来我们再来把判断条件判断条件抽取成方法提高可读性

text
1 2 3 4 5 6 7 8 9 10 11
double disabilityAmount () {
   if(isNotEligibleForDisableility()) {
      return 0;
   }
   // 省略...
}

boolean isNotEligibleForDisableility() {
   return _seniortiy <2 || _monthsDisabled > 12 || _isPartTime;
}

举例2:

text
1 2 3 4 5 6 7
if(onVacation()) {
   if(lengthOfService() > 10) {
      return 2;
   }
}
return 1;

合并之后的代码

text
1 2 3 4 5
if(onVacation() && lengthOfService() > 10){
   return 2
}
return 1;

接着我们可以使用三元操作符更加简化,修改后的代码:

text
1 2
return onVacation() && lengthOfService() > 10 ? 2 : 1;

3.提前判断返回

如下语句

text
1 2 3 4 5 6
if(condition){
   //dost
}else{
   return ;
}

改为

text
1 2 3 4 5
if(!condition){
   return ;
}
//dost

避免一些不必要的分支,让代码更精炼。

4.引入断言工具类

比如下面这段代码:

text
1 2 3 4 5 6 7
public void getProjectLimit(String project){
   if(project == null){
      throw new RuntimeException("project can not null");
   }
   doSomething();
}

加入Spring的断言后的代码 或者自定义断言 Assert.java

text
1 2 3 4 5
public void getProjectLimit(String project){
   Assert.notNull(project,"project can not null");
   doSomething();
}

5.善用 Optional

在项目中,总少不了一些非空的判断,可能大部分人还是如下的用法

text
1 2 3 4 5 6
if(null == user){
   //action1
}else{
   //action2
}

这时候该掏出Optional这个秘密武器了,它可以让非空校验更加优雅,间接的减少if操作。没了解过Optional的同学可自行Google,这里就不再赘述。

text
1 2 3 4 5 6 7 8
// 如果dtolgetProductType()为空 则默认为0
Integer productType = Optional.ofNullable(dto.getProductType()).orElse(0)

user = Optional.ofNullable(user).orElse(new User());

Optional<User> userOptional = Optional.ofNullable(user);
userOptional.map(action1).orElse(action2);

上面的代码跟第一段是等效的,通过一些新特性让代码更加紧凑。

6.使用枚举

text
1 2 3 4 5 6 7 8 9 10
public enum Status {
   NEW(0),RUNNABLE(1),RUNNING(2),BLOCKED(3),DEAD(4);

   public int statusCode;

   Status(int statusCode){
      this.statusCode = statusCode;
   }
}

那么我们在使用的时候就可以直接通过枚举调用了。
int statusCode = Status.valueOf(“NEW”).statusCode;
优雅的解决了下面代码赋值的方式

text
1 2 3 4 5 6
if(param.equals("NEW")){
   statusCode = 0;
}else if(param.equals("RUNNABLE")){
   statusCode = 1;
}

使用枚举优化if else实现2

7.枚举多态

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
int attackPower(Attacker attacker) {
   return AttackerType.valueOf(attacker.getType()).getAttackPower();
}

enum AttackerType {
   Bartizan("箭塔") {
      @Override
      public int getAttackPower() {
         return 100;
      }
   },
   Archer("弓箭手") {
      @Override
      public int getAttackPower() {
         return 50;
      }
   },
   Tank("坦克") {
      @Override
      public int getAttackPower() {
         return 800;
      }
   };

   private String label;

   Attacker(String label) {
      this.label = label;
   }

   public String getLabel() {
      return label;
   }

   public int getAttackPower() {
      throw new RuntimeException("Can not support the method");
   }
}

8.类多态

if else 示例1:

text
1 2 3 4 5 6 7 8 9
  String medalType = "guest";
  if ("guest".equals(medalType)) {
     System.out.println("嘉宾勋章");
   } else if ("vip".equals(medalType)) {
     System.out.println("会员勋章");
  } else if ("guard".equals(medalType)) {
     System.out.println("守护勋章");
  }

多态优化:

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//勋章接口
public interface IMedalService {
   void showMedal();
}

//守护勋章策略实现类
public class GuardMedalServiceImpl implements IMedalService {
   @Override
   public void showMedal() {
      System.out.println("展示守护勋章");
   }
}
//嘉宾勋章策略实现类
public class GuestMedalServiceImpl implements IMedalService {
   @Override
   public void showMedal() {
      System.out.println("嘉宾勋章");
   }
}

//勋章服务工厂类
public class MedalServicesFactory {

   private static final Map<String, IMedalService> map = new HashMap<>();
   static {
      map.put("guard", new GuardMedalServiceImpl());
      map.put("vip", new VipMedalServiceImpl());
      map.put("guest", new GuestMedalServiceImpl());
   }
   public static IMedalService getMedalService(String medalType) {
      return map.get(medalType);
   }
}

示例2:

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
int attackPower(Attacker attacker) {
   return attacker.getAttackPower();
}

interface Attacker {
   default int getAttackPower() {
      throw new RuntimeException("Can not support the method");
   }
}

class Bartizan implements Attacker {
   public int getAttackPower() {
      return 100 * getLevel();
   }
}

class Archer implements Attacker {
   public int getAttackPower() {
      return 50 * getLevel();
   }
}

class Tank implements Attacker {
   public int getAttackPower() {
      return 800 * getLevel();
   }
}

9.表驱动法

来自Google的解释:表驱动法是一种编程模式,它的本质是,从表里查询信息来代替逻辑语句(if,case)。下面看一个案例,通过月份来获取当月的天数(仅作为案例演示,获取2月份的数据不严谨),普通做法:

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int getMonthDays(int month){
    switch(month){
        case 1:return 31;break;
        case 2:return 29;break;
        case 3:return 31;break;
        case 4:return 30;break;
        case 5:return 31;break;
        case 6:return 30;break;
        case 7:return 31;break;
        case 8:return 31;break;
        case 9:return 30;break;
        case 10:return 31;break;
        case 11:return 30;break;
        case 12:return 31;break;
        default:return 0;
    }
}

表驱动法实现方式

text
1 2 3 4 5
int monthDays[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int getMonthDays(int month){
    return monthDays[--month];
}

其实这里的表就是数组而已,通过直接查询数组来获得需要的数据,那么同理,Map之类的容器也可以成为我们编程概念中的表。

text
1 2 3 4 5 6 7 8 9 10
Map<?, Function<?> action> actionsMap = new HashMap<>();

// 初试配置对应动作
actionsMap.put(value1, (someParams) -> { doAction1(someParams)});
actionsMap.put(value2, (someParams) -> { doAction2(someParams)});
actionsMap.put(value3, (someParams) -> { doAction3(someParams)});
 
// 省略 null 判断
actionsMap.get(param).apply(someParams);

10. Map + Java8 函数式接口

通过Java8的lambda表达式,我们把需要执行东西存进value中,调用的时候通过匹配key的方式进行。

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
@Service
public class QueryGrantTypeService {

   @Autowired
   private GrantTypeSerive grantTypeSerive;

   private final Map<String, Function<String, String>> grantTypeMap = new HashMap<>();

   /**
    * 初始化业务分派逻辑,代替了if-else部分
    * key: 优惠券类型
    * value: lambda表达式,最终会获得该优惠券的发放方式
    */
   @PostConstruct
   public void dispatcherInit() {
      grantTypeMap.put("红包", resourceId -> grantTypeSerive.redPaper(resourceId));
      grantTypeMap.put("购物券", resourceId -> grantTypeSerive.shopping(resourceId));
      grantTypeMap.put("vip会员", resourceId -> grantTypeSerive.vip(resourceId));
   }

   public String getResult(String resourceType, String resourceId) {
      // Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType
      Function<String, String> result = grantTypeMap.get(resourceType);
      if (result != null) {
         // 传入 resourceId 执行这段表达式获得String型的grantType
         return result.apply(resourceId);
      }
      return "查询不到该优惠券的发放方式";
   }
}

@Service
public class GrantTypeSerive {

   public String redPaper(String resourceId) {
      //红包的发放方式
      return "每周末9点发放";
   }

   public String shopping(String resourceId) {
      //购物券的发放方式
      return "每周三9点发放";
   }

   public String vip(String resourceId) {
      //qq会员的发放方式
      return "每周一0点开始秒杀";
   }
}

调用:

text
1 2 3 4 5 6 7 8 9 10 11 12
@RestController
public class GrantTypeController {

   @Autowired
   private QueryGrantTypeService queryGrantTypeService;

   @PostMapping("/grantType")
   public String test(String resourceName){
      return queryGrantTypeService.getResult(resourceName);
   }
}

原文地址:https://blog.csdn.net/fzy629442466/article/details/85762748?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168474993416800184197548%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168474993416800184197548&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-3-85762748-null-null.142^v87^insert_down1,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96