mvc做的游戏网站代码,wordpress 链接 拼音,wordpress自定义附近上传路径,兰州市做网站的公司背景:
下单时有很多情况,有的是用户下单,有的是卡密下单,有的是下游下单,有的是需要唤起支付,有的不需要支付,这样就需要写很多下单接口,下面使用策略模式优化这种情况
代码结构
com.example.order
├── controller
│ └── OrderController.java
├── service
│ …背景:
下单时有很多情况,有的是用户下单,有的是卡密下单,有的是下游下单,有的是需要唤起支付,有的不需要支付,这样就需要写很多下单接口,下面使用策略模式优化这种情况
代码结构
com.example.order
├── controller
│ └── OrderController.java
├── service
│ ├── OrderService.java
│ └── impl
│ └── OrderServiceImpl.java
├── strategy
│ ├── OrderStrategy.java
│ ├── factory
│ │ └── OrderStrategyFactory.java
│ ├── impl
│ │ ├── UserOrderStrategy.java
│ │ └── CardOrderStrategy.java
├── dto
│ └── OrderDTO.java
├── vo
│ ├── UserOrderVO.java
│ └── CardOrderVO.java
├── mapper
│ ├── OrderMapper.java
│ └── xml
│ └── OrderMapper.xml
├── domain
│ └── OrderDO.javaRestController
RequestMapping(/orders)
public class OrderController {Autowiredprivate OrderService orderService;PostMapping(/create)public ResponseEntity? createOrder(RequestBody OrderDTO orderDTO) {Object orderVO orderService.createOrder(orderDTO);return new ResponseEntity(orderVO, HttpStatus.OK);}
}Service
public class OrderServiceImpl implements OrderService {OverrideSuppressWarnings(unchecked)public T T createOrder(OrderDTO orderDTO) {OrderStrategyT strategy OrderStrategyFactory.getStrategy(orderDTO.getOrderType());return strategy.createOrder(orderDTO);}
}策略类接口
public interface OrderStrategyT {T createOrder(OrderDTO orderDTO);
}用户下单策略类
public class UserOrderStrategy implements OrderStrategyUserOrderVO {Overridepublic UserOrderVO createOrder(OrderDTO orderDTO) {// 用户下单的具体处理逻辑// 生成订单并保存到数据库// 返回UserOrderVO对象UserOrderVO userOrderVO new UserOrderVO();// 设置相关属性return userOrderVO;}
}卡密下单策略类
public class CardOrderStrategy implements OrderStrategyCardOrderVO {Overridepublic CardOrderVO createOrder(OrderDTO orderDTO) {// 卡密下单的具体处理逻辑// 生成订单并保存到数据库// 返回CardOrderVO对象CardOrderVO cardOrderVO new CardOrderVO();// 设置相关属性return cardOrderVO;}
}策略类工厂
import java.util.HashMap;
import java.util.Map;public class OrderStrategyFactory {private static final MapString, OrderStrategy? strategies new HashMap();static {strategies.put(USER_ORDER, new UserOrderStrategy());strategies.put(CARD_ORDER, new CardOrderStrategy());// 初始化其他订单类型的策略}SuppressWarnings(unchecked)public static T OrderStrategyT getStrategy(String orderType) {OrderStrategy? strategy strategies.get(orderType);if (strategy null) {throw new IllegalArgumentException(Unknown order type: orderType);}return (OrderStrategyT) strategy;}
}public class UserOrderVO {private String orderId;private String status;private String userSpecificInfo;// 其他用户订单特有属性// Getters and Setters
}public class CardOrderVO {private String orderId;private String status;private String cardCode;// 其他卡密订单特有属性// Getters and Setters
}public interface OrderMapper {void insertOrder(OrderDO orderDO);// 其他数据库操作方法
}mapper namespacecom.example.mapper.OrderMapperinsert idinsertOrder parameterTypeOrderDOINSERT INTO orders (order_id, user_id, order_type, payment_type, status)VALUES (#{orderId}, #{userId}, #{orderType}, #{paymentType}, #{status})/insert!-- 其他数据库操作的XML定义 --
/mapper