springboot策略模式代码demo


image.png

package com.example.demo.service;

public interface AnalysisService {


    /**
     * 获取数据分析厂商
     * @return
     */
    String getAnalysisCompany();

    /**
     * 人脸检索
     * @return
     */
    String findFace();

    /**
     * 机动车检索
     * @return
     */
    String findMotorVehicle();

}

package com.example.demo.service.impl;

import com.example.demo.service.AnalysisService;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractAnalysisService implements AnalysisService {


    private static final Map<String,AnalysisService> map = new HashMap<>();

    public static final String HUAWEI_NAME = "hw";
    public static final String YITU_NAME = "yt";

    @PostConstruct
    public void init(){
        map.put(getAnalysisCompany(),this);
    }

    public static AnalysisService getService(String company){
        AnalysisService service = map.get(company);
        if(service == null){
            service = map.get(HUAWEI_NAME);
        }
        return service;
    }

}

package com.example.demo.service.impl;

import org.springframework.stereotype.Service;

@Service
public class YtAnalysisServiceImpl extends AbstractAnalysisService{

    @Override
    public String getAnalysisCompany() {
        return AbstractAnalysisService.YITU_NAME;
    }

    @Override
    public String findFace() {
        System.out.println("调用了依图人脸检索");
        return "调用了依图人脸检索";
    }

    @Override
    public String findMotorVehicle() {
        System.out.println("调用了依图车辆检索");
        return "调用了依图车辆检索";
    }
}

package com.example.demo.service.impl;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnMissingClass(value = "com.example.demo.service.impl.Hw20AnalysisServiceImpl")
public class HwAnalysisServiceImpl extends AbstractAnalysisService {

    @Override
    public String getAnalysisCompany() {
        return AbstractAnalysisService.HUAWEI_NAME;
    }

    @Override
    public String findFace() {
        System.out.println("调用了华为人脸检索");
        return "调用了华为人脸检索";
    }

    @Override
    public String findMotorVehicle() {
        System.out.println("调用了华为车辆检索");
        return "调用了华为车辆检索";
    }


    public void httpClient(){

    }
}


package com.example.demo.service.impl;

import org.springframework.stereotype.Service;

@Service
public class Hw20AnalysisServiceImpl extends HwAnalysisServiceImpl{



    @Override
    public String findFace() {
        return "调用了华为2.0的搜人接口";
    }

    @Override
    public String getAnalysisCompany() {
        return super.getAnalysisCompany();
    }

    @Override
    public String findMotorVehicle() {
        return super.findMotorVehicle();
    }
}


使用

@RestController
public class AnalysisController {



    @GetMapping("/findFace")
    private String findFace(String company){
        AnalysisService analysisService = AbstractAnalysisService.getService(company);
        return analysisService.findFace();
    }

    @GetMapping("/findMotorVehicle")
    private String findMotorVehicle(String company){
        AnalysisService analysisService = AbstractAnalysisService.getService(company);
        return analysisService.findMotorVehicle();
    }
}


代码:https://gitee.com/LanMeiWeiQingXin/dymamicIoc

微信扫描二维码,关注后回复,获取精华资料!

1、回复「书单」:免费获取百本「豆瓣」高分好书。
2、回复「赚钱」:领取实用的36个赚钱小项目。
3、回复「TED」:送你100场TED最受欢迎的演讲,感受最顶尖的思想。
4、回复「学习」:免费获赠英语7000单词速记法(价值200元,很好用),四六级轻松过;
5、回复「PPT」:送你500套好看又实用的PPT模板,让你的PPT颜值爆表]
6、回复「88」:java精品案例,微服务架构Springboot项目实战

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×