代码学习

方法一:枚举

1、使用枚举前:

1
2
3
4
5
6
7
8
9
10
11
12
String orderStatusDes;
if ("1".equals(orderStatus)) {
orderStatusDes = "订单未支付";
} else if ("2".equals(orderStatus)) {
orderStatusDes = "订单已支付";
} else if ("3".equals(orderStatus)) {
orderStatusDes = "订单已发货";
} else if ("4".equals(orderStatus)) {
orderStatusDes = "订单已签收";
} else if ("5".equals(orderStatus)) {
orderStatusDes = "订单已评价";
}

2.使用枚举后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Getter
@AllArgsConstructor
public enum OrderStatusEnum {
UN_PAID("1","订单未支付"),
PAIDED("2","订单已支付"),
SENDED("3","订单已发货"),
SINGED("4","订单已签收"),
EVALUATED("5","订单已评价");
private String status;
private String statusDes;
static OrderStatusEnum of(String status) {
for (OrderStatusEnum statusEnum : OrderStatusEnum.values()) {
if (statusEnum.getStatus().equals(status)) {
return statusEnum;
}
}
return null;
}
}

3.优化调用方法

1
String orderStatusDes = OrderStatusEnum.of(orderStatus).getStatusDes();

方法二:Optional 判空

1.未使用Optional的代码:

1
2
3
4
5
6
Order order = getOrderById(id);
if (order == null) {
return "-1";
} else {
return order.getOrderStatus();
}

2.Optional :

1
return Optional.ofNullable(order).map(o -> o.getOrderStatus()).orElse("-1");

方法三:表驱动法

1.未使用优化方法的如下:

1
2
3
4
5
6
7
8
9
10
11
if ("code1".equals(action)) {
doAction1();
} else if ("code2".equals(action)) {
doAction2();
} else if ("code3".equals(action)) {
doAction3();
} else if ("code4".equals(action)) {
doAction4();
} else if ("code5".equals(action)) {
doAction5();
}

2.优化方法的如下:

1
2
3
4
5
6
7
Map<String, Function<?> action> actionMap = new HashMap<>();
action.put("code1",() -> {doAction1()});
action.put("code2",() -> {doAction2()});
action.put("code3",() -> {doAction3()});
action.put("code4",() -> {doAction4()});
action.put("code5",() -> {doAction5()});
// 使用actionMap.get(action).apply();

方案四:策略模式 + 工厂方法

策略模式 + 工厂方法是解决 if…else 用得非常多的方案,它和上面的表驱动法有点儿类似。使用策略模式 + 工厂方法分为几个步骤,以上面例子为例:

  • 把条件模块抽象为一个公共的接口,策略接口
    1
    2
    3
    4
    public interface ActionService {
    void doAction();
    }

  • 根据每个逻辑,定义出自己具体的策略实现类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class ActionService1 implements ActionService{
    public void doAction() {
    //do something
    }
    }

    public class ActionService2 implements ActionService{
    public void doAction() {
    //do something
    }
    }

    // 省略其他策略

  • 工厂类,统一调度,用来管理这些策略
    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
    public class ActionServiceFactory {
    private ActionServiceFactory(){

    }

    private static class SingletonHolder{
    private static ActionServiceFactory instance=new ActionServiceFactory();
    }

    public static ActionServiceFactory getInstance(){
    return SingletonHolder.instance;
    }

    private static final Map<String,ActionService> ACTION_SERVICE_MAP = new HashMap<String, ActionService>();

    static {
    ACTION_SERVICE_MAP.put("action1",new ActionService1());
    ACTION_SERVICE_MAP.put("action2",new ActionService2());
    ACTION_SERVICE_MAP.put("action3",new ActionService3());
    ACTION_SERVICE_MAP.put("action4",new ActionService4());
    ACTION_SERVICE_MAP.put("action5",new ActionService5());
    }

    public static ActionService getActionService(String actionCode) {
    ActionService actionService = ACTION_SERVICE_MAP.get(actionCode);
    if (actionService == null) {
    throw new RuntimeException("非法 actionCode");
    }
    return actionService;
    }

    public void doAction(String actionCode) {
    getActionService(actionCode).doAction();
    }
    }
  • 单例模式实现工厂类。
    1
    ActionServiceFactory.getInstance().doAction("action1");