需求背景
在实际项目中,好比在一个简单的订单处理系统,其中订单有不同的状态(比如新建、已支付、已发货、已收货等),为了实现基于状态机的逻辑处理,我们可以通过switch(状态)去对应不同状态的处理逻辑。
1 public String process2() { 2 switch (status) { 3 case NEW: 4 return handleNewOrder(); 5 case PAYMENT_RECEIVED: 6 return handlePayment(); 7 case SHIPPED: 8 return handleShipment(); 9 case DELIVERED: 10 return handleDelivered(); 11 default: 12 return "Invalid order"; 13 } 14 }
但有种更优雅的做法是通过ImmutableMap<Integer, Function<Object, String>> 的方式去实现。
1 public static final ImmutableMap<OrderStatus , Function<Order, String>> STATUS_HANDLERS = 2 ImmutableMap.<OrderStatus, Function<Order, String>>builder() 3 .put(OrderStatus.NEW, Order::handleNewOrder) 4 .put(OrderStatus.PAYMENT_RECEIVED, Order::handlePayment) 5 .put(OrderStatus.SHIPPED, Order::handleShipment) 6 .put(OrderStatus.DELIVERED, Order::handleDelivered) 7 .build(); 8 9 public String process() { 10 Function<Order, String> handler = STATUS_HANDLERS.get(status); 11 if (handler == null) { 12 return "Invalid order"; 13 } 14 return handler.apply(this); 15 }
完整代码如下
订单状态枚举
1 public enum OrderStatus { 2 /** 3 * 新建 4 * 已付款 5 * 已发货 6 * 已收货 7 */ 8 NEW, 9 PAYMENT_RECEIVED, 10 SHIPPED, 11 DELIVERED 12 }
订单类
1 @Data 2 public class Order { 3 private final String orderId; 4 private final OrderStatus status; 5 6 public Order(String orderId, OrderStatus status) { 7 this.orderId = orderId; 8 this.status = status; 9 } 10 11 public static final ImmutableMap<OrderStatus , Function<Order, String>> STATUS_HANDLERS = 12 ImmutableMap.<OrderStatus, Function<Order, String>>builder() 13 .put(OrderStatus.NEW, Order::handleNewOrder) 14 .put(OrderStatus.PAYMENT_RECEIVED, Order::handlePayment) 15 .put(OrderStatus.SHIPPED, Order::handleShipment) 16 .put(OrderStatus.DELIVERED, Order::handleDelivered) 17 .build(); 18 19 public String process() { 20 Function<Order, String> handler = STATUS_HANDLERS.get(status); 21 if (handler == null) { 22 return "Invalid order"; 23 } 24 return handler.apply(this); 25 } 26 27 public String process2() { 28 switch (status) { 29 case NEW: 30 return handleNewOrder(); 31 case PAYMENT_RECEIVED: 32 return handlePayment(); 33 case SHIPPED: 34 return handleShipment(); 35 case DELIVERED: 36 return handleDelivered(); 37 default: 38 return "Invalid order"; 39 } 40 } 41 42 private String handleNewOrder() { 43 // Handle new order logic here 44 return "Order " + orderId + " 新建"; 45 } 46 47 private String handlePayment() { 48 // Handle payment logic here 49 return "Order " + orderId + " 已支付"; 50 } 51 52 private String handleShipment() { 53 // Handle shipment logic here 54 return "Order " + orderId + " 已发货"; 55 } 56 57 private String handleDelivered() { 58 // Handle delivered logic here 59 return "Order " + orderId + " 已收获"; 60 } 61 62 public static void main(String[] args) { 63 Order order = new Order("12345", OrderStatus.SHIPPED); 64 //if else方式 65 String result2 = order.process2(); 66 //map + Function的方式 67 String result = order.process(); 68 69 System.out.println(result); 70 System.out.println(result2); 71 } 72 }
运行结果
标签:Function,map,return,String,else,public,OrderStatus,put,Order From: https://www.cnblogs.com/liaowenhui/p/17373943.html