1. Business background

There are individual interface businesses to provide customized development for each province

Second, think before development

  • 1, the original business logic is complex enough, if you add if else if code is not easy to maintain, also not beautiful

  • 2, because it is other groups to call this service, if you have to code every time, development will be very disgusting, what good method….

  • 3, what can be installed to force and can solve the existing problem (programmer’s high level, and practical and can install to force)

Three, the selection

  • Provides a unified interface

  • Use Spring Bean + policy pattern to solve different business of different platforms

  • Use the enumeration + facade pattern to provide a uniform processing entry point

Four, code implementation

  • Provide the interface
Public interface IFusionLoginService {/** * Auth Login related * @param Request * @param authInfo * @return */ Boolean auth(HttpServletRequest request, UserToken authInfo); /** * forgetPwd * @param request * @return */ default Boolean forgetPwd(HttpServletRequest request) {return true; } /** * * @param fusionLogin * @return */ boolean login(FusionLoginVO fusionLogin); }Copy the code
  • Implementation class, company code does not give specific implementation
@Slf4j
@Component(value = FusionConstant.FUSION_LOGIN + FusionConstant.GD)
public class GdFusionLoginServiceImpl implements IFusionLoginService {

   

    @Override
    public boolean auth(HttpServletRequest request, UserToken authInfo) {
         //todo
    }
    

    @Override
    public boolean forgetPwd(HttpServletRequest request) {
         //todo
        return false;
    }

    @Override
    public boolean login(FusionLoginVO fusionLogin) {
       //todo
    }
Copy the code
  • The facade pattern provides uniform processing of beans
@slf44j @Component public Class FusionLoginProvider {// The key is the name of the bean @autowired private Map<String, IFusionLoginService> fusionLoginServiceMap; @Autowired private ISysConfigService sysConfigService; private String getPlatformKey() { return FusionConstant.FUSION_LOGIN + sysConfigService.getString(FusionConstant.FUSION_PLATFORM_CONTROL); } private IFusionLoginService getFusionLogin() { if (CollectionUtil.isEmpty(fusionLoginServiceMap)) { return null; } String platformKey = getPlatformKey(); IFusionLoginService fusionLoginService = fusionLoginServiceMap.get(platformKey); if (Objects.isNull(fusionLoginService)) { return null; } return fusionLoginService; } /** * @return false */ public boolean isFusion() { IFusionLoginService fusionLoginService = getFusionLogin(); If (objects.isnull (fusionLoginService)) {return false; } return true; } public Boolean handleLogin(FusionLoginVO fusionLogin, FusionLoginHandler handler) { IFusionLoginService fusionLoginService = getFusionLogin(); If (objects.isnull (fusionLoginService)) {return true if (objects.isnull (fusionLoginService)); } return FusionLoginHandler.handle(handler, fusionLoginService, fusionLogin); }}Copy the code
  • Enumeration class, the processing class of concrete logic
public enum FusionLoginHandler { AUTH { @Override boolean handle(IFusionLoginService fusionLoginService, FusionLoginVO fusionLogin) { return fusionLoginService.auth(fusionLogin.getRequest(), fusionLogin.getAuthInfo()); } }, FORGET_PWD { @Override boolean handle(IFusionLoginService fusionLoginService, FusionLoginVO fusionLogin) { return fusionLoginService.forgetPwd(fusionLogin.getRequest()); } }, LOGIN { @Override boolean handle(IFusionLoginService fusionLoginService, FusionLoginVO fusionLogin) { return fusionLoginService.login(fusionLogin); }}; abstract boolean handle(IFusionLoginService fusionLoginService, FusionLoginVO fusionLogin); public static boolean handle(FusionLoginHandler handler, IFusionLoginService fusionLoginService, FusionLoginVO fusionLogin) { return handler.handle(fusionLoginService, fusionLogin); }}Copy the code
  • Specific invocation mode
//
fusionLoginProvider.handleLogin(new FusionLoginVO(request,authInfo), FusionLoginHandler.AUTH);
//
fusionLoginProvider.handleLogin(new FusionLoginVO(request,null),FusionLoginHandler.FORGET_PWD);
//
fusionLoginProvider.handleLogin(new FusionLoginVO(fusionDataReqDTO),FusionLoginHandler.LOGIN);
Copy the code

Five,

  • The code has been put on line in June. After more than a month of production inspection, there is no problem at present

  • If the business to modify on the original code, I think must consider to be clear about, good extension, good maintenance, good coordination development, if don’t meet these content, and the business based on the change of different scenarios for different logic, must stop to think, can’t pick up the keyboard is lu, even out now, later became a business or want to return Work.

  • This transformation is not overnight, but continuous grinding, not for the sake of technology and technology, but to the best fit.

  • Do technology to code to have a heart of awe, continuous accumulation, continuous hone and continuous improvement.