前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《设计模式篇:》《装饰者设计模式》防止service二次注入,controller调用的值不同,自己弄个壳子

《设计模式篇:》《装饰者设计模式》防止service二次注入,controller调用的值不同,自己弄个壳子

作者头像
发布2020-12-03 14:28:39
4420
发布2020-12-03 14:28:39
举报
文章被收录于专栏:后端JavaEE后端JavaEE

概述

通过调用controller的search与change方法来实现使用壳子切换service的功能。 1.controller使用枚举调用FaceServiceWapper里面的:查找当前service、切换当前的service的功能 2.FaceServiceWapper继承FaceService接口 3.AliFaceServiceImpl与SelfFaceServiceImpl继承FaceMethod接口


一、枚举

代码语言:javascript
复制
package com.qf.face.service.config;

/**
 * zt
 * 2020/12/1
 * 20:03
 */
public enum  ImplType {

    SELF(0,"com.qf.face.service.impl.SelfFaceService"),
    BAIDU(1,"com.qf.face.service.impl.BaiDuFaceServiceImpl"),
    ALIBABA(2,"com.qf.face.service.impl.AliFaceServiceImpl");

    private Integer type;
    public String className;
    ImplType(Integer type,String className){
        this.type = type;
        this.className = className;
    }

    public static ImplType match(Integer type) throws Exception {
        switch (type){
            case 0:return SELF;
            case 1:return BAIDU;
            case 2:return ALIBABA;
            default:throw new Exception("不支持的类型");

        }
    }













}

二、controller

代码语言:javascript
复制
package com.qf.face.service.controller;

import com.qf.face.service.base.FaceService;
import com.qf.face.service.config.ImplType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * zt
 * 2020/11/30
 * 21:02
 */
@RestController
@RequestMapping("face")
public class FaceController {

    @Resource
    FaceService faceService;

    //注册人脸

    //人脸操作

    //对比两个图片是不是同一个人
    @GetMapping("search")
    public String searchFace(){
        return faceService.searchFace("");
    }

    @GetMapping("change/{type}")
    public String change(@PathVariable(value = "type") Integer type){
        try {
            ImplType implType = ImplType.match(type);
            faceService.changeExcutor(implType);
            return implType.className;
        } catch (Exception e) {
            e.printStackTrace();
            return "暂时不支持类型";
        }

    }















}

三、另一个service:AliFaceServiceImpl 与原有的service:SelfFaceServiceImpl (用壳子来回切换它们)

代码语言:javascript
复制
package com.qf.face.service.impl;

import com.qf.face.service.parent.FaceMethod;

/**
 * zt
 * 2020/12/1
 * 20:06
 */
public class AliFaceServiceImpl implements FaceMethod {

    @Override
    public boolean save(String base64, String uid) {
        return false;
    }

    @Override
    public String searchFace(String base64) {
        return "Ali";
    }
}
代码语言:javascript
复制
package com.qf.face.service.impl;

import com.qf.face.service.parent.FaceMethod;

/**
 * zt
 * 2020/12/1
 * 19:59
 */
public class SelfFaceServiceImpl implements FaceMethod {

    @Override
    public boolean save(String base64, String uid) {
        return true;
    }

    @Override
    public String searchFace(String base) {
        return "self";
    }
}

四、接口FaceMethod与接口FaceService(提供:判断、查找)

代码语言:javascript
复制
package com.qf.face.service.parent;

/**
 * zt
 * 2020/12/1
 * 20:13
 */
public interface FaceMethod {
    boolean save(String base64,String uid);
    String searchFace(String base64);
}
代码语言:javascript
复制
package com.qf.face.service.base;

import com.qf.face.service.config.ImplType;
import com.qf.face.service.parent.FaceMethod;

/**
 * zt
 * 2020/11/30
 * 21:12
 */
public interface FaceService extends FaceMethod {
    void changeExcutor(ImplType type);
}

五、切换service的壳子FaceServiceWapper

代码语言:javascript
复制
package com.qf.face.service.warpper;

import com.qf.face.service.base.FaceService;
import com.qf.face.service.config.ImplType;
import com.qf.face.service.impl.SelfFaceServiceImpl;
import com.qf.face.service.parent.FaceMethod;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;

/**
 * zt
 * 2020/12/1
 * 19:58
 */
@Service
public class FaceServiceWapper implements FaceService{

    FaceMethod excutor;
    {
        excutor= new SelfFaceServiceImpl();
    }
    @Override
    public boolean save(String base64, String uid) {

        return excutor.save(base64, uid);
    }

    @Override
    public String searchFace(String base) {
        return excutor.searchFace(base);

    }

    @Override
    public void changeExcutor(ImplType type){
        try {
            excutor = (FaceMethod) Class.forName(type.className).getConstructor().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-12-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 一、枚举
  • 二、controller
  • 三、另一个service:AliFaceServiceImpl 与原有的service:SelfFaceServiceImpl (用壳子来回切换它们)
  • 四、接口FaceMethod与接口FaceService(提供:判断、查找)
  • 五、切换service的壳子FaceServiceWapper
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档