概述
通过调用controller的search与change方法来实现使用壳子切换service的功能。
1.controller使用枚举调用FaceServiceWapper里面的:查找当前service、切换当前的service的功能
2.FaceServiceWapper继承FaceService接口
3.AliFaceServiceImpl与SelfFaceServiceImpl继承FaceMethod接口
一、枚举
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
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 (用壳子来回切换它们)
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";
}
}
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(提供:判断、查找)
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);
}
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
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();
}
}
}