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

编程入门 行业动态 更新时间:2024-10-24 03:25:47

《设计<a href=https://www.elefans.com/category/jswz/34/1771241.html style=模式篇:》《装饰者设计模式》防止service二次注入,controller调用的值不同,自己弄个壳子"/>

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

概述

通过调用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 {@ResourceFaceService 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 {@Overridepublic boolean save(String base64, String uid) {return false;}@Overridepublic 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 {@Overridepublic boolean save(String base64, String uid) {return true;}@Overridepublic 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();}@Overridepublic boolean save(String base64, String uid) {return excutor.save(base64, uid);}@Overridepublic String searchFace(String base) {return excutor.searchFace(base);}@Overridepublic 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();}}}

更多推荐

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

本文发布于:2024-02-10 19:08:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1676840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   壳子   弄个   controller   service

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!