流程没有判断整合用门面有用状态。
门面(外观)模式:https://www.cnblogs.com/zhenghengbin/p/9304124.html
解释:把不同功能的方法封装到类里面,新建类方法里面new对象,统一调用方法。
代码:mayikt_facade
四步的流程应该如何重构代码:
用户下单成功后,有那些操作?
1.增加支付回调接口日志
2.修改订单数据库状态为已经成功
3.调用积分服务接口
4.调用消息服务平台服务接口
这四步放在一起的话代码的冗余度是很高的导致代码很复杂。
作用就是用一个接口将复杂的流程统一封装为简单的流程。
开闭原则。
----------------------------------------------------------------01--------------------------------------------------------------------------------------------------
拆分:
package com.mayikt.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author
* @title: LogService
* @description:
* @date 2019/5/2518:45
*/
@Component
@Slf4j
public class LogService {
public void logService(Map<String, String> verifySignature) {
// 1.第一步打印日志信息
String orderId = verifySignature.get("orderId"); // 获取后台通知的数据,其他字段也可用类似方式获取
String respCode = verifySignature.get("respCode");
log.info("第一个模块>>>orderId:{},respCode:{}", orderId, respCode);
}
}
package com.mayikt.mapper;
/**
* @author
* @title: PaymentTransactionMapper
* @description:
* @date 2019/5/2317:38
*/
public interface PaymentTransactionMapper {
void updatePaymentStatus();
}
package com.mayikt.service;
import com.mayikt.mapper.PaymentTransactionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author
* @title: PaymentService
* @description:
* @date 2019/5/2518:46
*/
@Slf4j
@Component
public class PaymentService {
public void updatePaymentStatus() {
// 2.修改订单状态为已经支付
new PaymentTransactionMapper() {
@Override
public void updatePaymentStatus() {
log.info("第二个模块>>>修改订单状态为已经支付>>>>>");
}
}.updatePaymentStatus();
}
}
package com.mayikt.utils;
import lombok.extern.slf4j.Slf4j;
/**
* @author
* @title: HttpClientUtils
* @description:
* @date 2019/5/2317:41
*/
@Slf4j
public class HttpClientUtils {
public static String doPost(String url, String text) {
log.info(">>>Url:{},text:{}", url, text);
return "success";
}
}
package com.mayikt.service;
import com.mayikt.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author
* @title: IntegralService
* @description:
* @date 2019/5/2518:47
*/
@Component
@Slf4j
public class IntegralService {
public void callIntegral() {
// 3.调用积分接口增加积分
HttpClientUtils.doPost("jifen.com", "积分接口");
log.info("第三个模块>>>调用积分接口打印日志>>>>>");
}
}
package com.mayikt.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author
* @title: MsgService
* @description:
* @date 2019/5/2518:48
*/
@Component
@Slf4j
public class MsgService {
public void msgService() {
log.info("第四个模块>>>调用消息模块打印日志>>>>>");
}
}
package com.mayikt.facade;
import com.mayikt.service.IntegralService;
import com.mayikt.service.LogService;
import com.mayikt.service.MsgService;
import com.mayikt.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author
* @title: PayCallbackFacade
* @description:
* @date 2019/5/2518:50
*/
@Component
public class PayCallbackFacade {
@Autowired
private LogService logService;
@Autowired
private PaymentService paymentService;
@Autowired
private IntegralService integralService;
@Autowired
private MsgService msgService;
public boolean callbackFacade(Map<String, String> verifySignature) {
logService.logService(verifySignature);
paymentService.updatePaymentStatus();
integralService.callIntegral();
msgService.msgService();
return true;
}
}
调用:
package com.mayikt.service;
import com.mayikt.facade.PayCallbackFacade;
import com.mayikt.mapper.PaymentTransactionMapper;
import com.mayikt.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
/**
* @author
* @title: PayCallbackService
* @description:
* @date 2019/5/2518:35
*/
@Slf4j
public class PayCallbackService {
// 用户下单成功后,有那些操作?
// 1.增加支付回调接口日志
// 2.修改订单数据库状态为已经成功
// 3.调用积分服务接口
// 4.调用消息服务平台服务接口
@Autowired
private PayCallbackFacade payCallbackFacade;
public boolean callback(Map<String, String> verifySignature) {
// // 1.第一步打印日志信息
// String orderId = verifySignature.get("orderId"); // 获取后台通知的数据,其他字段也可用类似方式获取
// String respCode = verifySignature.get("respCode");
// log.info("orderId:{},respCode:{}", orderId, respCode);
// // 2.修改订单状态为已经支付
// new PaymentTransactionMapper() {
// @Override
// public void updatePaymentStatus() {
// log.info(">>>修改订单状态为已经支付>>>>>");
// }
// }.updatePaymentStatus();
// // 3.调用积分接口增加积分
// HttpClientUtils.doPost("jifen.com", "积分接口");
// // 4.调用消息服务平台提示
// HttpClientUtils.doPost("msg.com", "调用消息接口");
boolean result = payCallbackFacade.callbackFacade(verifySignature);
return result;
}
}
写controller
@RestController
public class PayCallbackController {
@Autowired
private PayCallbackService payCallbackService;
@RequestMapping("/payCallback")
public String payCallback(Map<String, String> verifySignature) {
payCallbackService.callback(verifySignature);
return "success";
}
}
--------------------------------02--------------------------------------------------------------
无
---------------------03-------------------------------------------------------------------------
状态模式:https://blog.csdn.net/nisen6477/article/details/91646600
要把这个方法抽取为一个类,不同的判断是不同的子类,并在方法所在的类变为成员变量。
public String orderState(String state) {
if (state.equals("0")) {
return "已经发货";
}
if (state.equals("1")) {
return "正在运送中...调用第三方快递接口 展示 运送信息";
}
if (state.equals("2")) {
return "正在派送中... 返回派送人员信息";
}
if (state.equals("3")) {
return "已经签收,提示给用户快递员评价";
}
if (state.equals("4")) {
return "拒绝签收, 重新开始申请退单";
}
if (state.equals("5")) {
return "订单交易失败,调用短信接口提示 ";
}
return "未找到对应的状态";
}
}
这个方法有很多的判断说明责任过于大了,这是很不好的代码。
这个类违背了“单一职责原则”,维护出错的风险过大,所以我们可以将这些分支变为一个个的类,增加时又不会影响其它类。然后状态的变化在各自的类中完成,这种解决方案就是状态设计模式。
当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为时,就可以考虑使用状态模式了。
策略模式解决的是共同的抽象的行为。
1、状态模式重点在各状态之间的切换从而做不同的事情,而策略模式更侧重于根据具体情况选择策略,并不涉及切换。
2、状态模式不同状态下做的事情不同,而策略模式做的都是同一件事,例如聚合支付平台,有支付宝、微信支付、银联支付,虽然策略不同,但最终做的事情都是支付,也就是说他们之间是可替换的。反观状态模式,各个状态的同一方法做的是不同的事,不能互相替换。
状态模式封装了对象的状态,而策略模式封装算法或策略。因为状态是跟对象密切相关的,它不能被重用;而通过从Context中分离出策略或算法,我们可以重用它们。
在状态模式中,每个状态通过持有Context的引用,来实现状态转移;但是每个策略都不持有Context的引用,它们只是被Context使用。
-----------------------------------------------------------------------04------------------------------------------------------------------
代码:meite_order
package com.mayikt.service;
/**
* @author
* @title: OrderService
* @description:
* @date 2019/5/2519:23
*/
public interface OrderState {
public void orderService();
}
这个order的实现类
package com.mayikt.service.impl;
import com.mayikt.service.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author
* @title: AlreadySignedOrderState
* @description:
* @date 2019/5/2519:27
*/
@Slf4j
@Component
public class AlreadySignedOrderState implements OrderState {
@Override
public void orderService() {
log.info(">>>切换为已经签收状态..");
}
}
package com.mayikt.service.impl;
import com.mayikt.service.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author
* @title: InTransitOrderState
* @description:
* @date 2019/5/2519:27
*/
@Slf4j
@Component
public class InTransitOrderState implements OrderState {
@Override
public void orderService() {
log.info(">>>切换为正在运送状态...");
}
}
package com.mayikt.service.impl;
import com.mayikt.service.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author
* @title: ShippedAlreadyOrderState
* @description:
* @date 2019/5/2519:25
*/
@Slf4j
@Component
public class ShippedAlreadyOrderState implements OrderState {
public void orderService() {
log.info(">>>切换为已经发货状态..");
}
}
如何切换:
package com.mayikt.service.context;
import com.mayikt.service.OrderState;
/**
* @author
* @title: ContextState
* @description:
* @date 2019/5/2519:29
*/
public class ContextState {
private OrderState orderState;
public ContextState(OrderState orderState) {
this.orderState = orderState;
}
public void switchStateOrder() {
orderState.orderService();
}
}
测试使用:
package com.mayikt.service;
import com.mayikt.service.context.ContextState;
import com.mayikt.service.impl.AlreadySignedOrderState;
import com.mayikt.service.impl.InTransitOrderState;
/**
* @author
* @title: Test
* @description:
* @date 2019/5/2519:28
*/
public class Test {
public static void main(String[] args) {
ContextState contextState = new ContextState(new AlreadySignedOrderState());
contextState.switchStateOrder();
}
}
如何使用:
package com.mayikt.controller;
import com.mayikt.service.OrderState;
import com.mayikt.service.context.ContextState;
import com.mayikt.service.impl.AlreadySignedOrderState;
import com.mayikt.utils.SpringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @title: OrderController
* @description:
* @date 2019/5/2519:35
*/
@RestController
public class OrderController {
@RequestMapping("/orderState")
public String orderState(String stateBeanId) {
//实体类的id和父类或者接口
OrderState orderState = SpringUtils.getBean(stateBeanId, OrderState.class);
ContextState contextState = new ContextState(orderState);
contextState.switchStateOrder();
return "success";
}
}
--------------------------------------------05--------------------------------------------------------


被折叠的 条评论
为什么被折叠?



