【笔记】SpringBoot项目下实践设计模式之工厂模式

149 阅读1分钟

示例结构(红框内的)

微信截图_20230423161023.png

接口

com.springcloud.business.service.ICarService

package com.springcloud.business.service;

/**
 * @author xbronze
 */
public interface ICarService {
    public String run();
}

实现类

com.springcloud.business.service.impl.BusServiceImpl

package com.springcloud.business.service.impl;

import com.springcloud.business.service.ICarService;
import org.springframework.stereotype.Service;

/**
 * @author: xbronze
 * @date: 2023-04-23 14:50
 * @description: TODO
 */
@Service
public class BusServiceImpl implements ICarService {
    @Override
    public String run() {
        return "大巴车一般要求时速控制在每小时80公里";
    }
}

com.springcloud.business.service.impl.SuperCarServiceImpl

package com.springcloud.business.service.impl;

import com.springcloud.business.service.ICarService;
import org.springframework.stereotype.Service;

/**
 * @author: xbronze
 * @date: 2023-04-23 14:51
 * @description: TODO
 */
@Service
public class SuperCarServiceImpl implements ICarService {
    @Override
    public String run() {
        return "超跑的车速轻松能达到每小时200公里";
    }
}

一个工厂注册类

com.springcloud.business.service.impl.CarServiceContent

package com.springcloud.business.service.impl;

import com.springcloud.business.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author: xbronze
 * @date: 2023-04-23 15:07
 * @description: TODO
 */
@Service
public class CarServiceContent {

    @Autowired
    private Map<String, ICarService> carServiceMap;

    public ICarService getCarService(String type) {
        if (carServiceMap.isEmpty()) {
            return null;
        }
        return this.carServiceMap.get(type);
    }
}

项目启动,系统会把ICarService的实现类都注入到carServiceMap,key值为实现类上@Service注解定义的value,如果没有显式的设置value,如示例上所示,那么默认value值为类名(首字母小写)。

Controller类VehicleController


/**
 * @author: xbronze
 * @date: 2023-04-23 14:54
 * @description: TODO
 */
@RestController
@RequestMapping("/vehicle")
public class VehicleController {

    @Autowired
    private IVehicleService vehicleService;

    @GetMapping("/{type}")
    public String vehicle(@PathVariable("type") String type){
        return vehicleService.choose(type);
    }
}

接口IVehicleService

package com.springcloud.business.service;

/**
 * @author: xbronze
 * @date: 2023-04-23 14:54
 * @description: TODO
 */
public interface IVehicleService {

    String choose(String type);
}

实现类VehicleServiceImpl

package com.springcloud.business.service.impl;

import com.springcloud.business.service.ICarService;
import com.springcloud.business.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: xbronze
 * @date: 2023-04-23 15:17
 * @description: TODO
 */
@Service
public class VehicleServiceImpl implements IVehicleService {

    @Autowired
    private CarServiceContent carServiceContent;

    @Override
    public String choose(String type) {
        ICarService carService = carServiceContent.getCarService(type);
        return carService.run();
    }
}

测试

微信截图_20230423164156.png

微信截图_20230423164134.png

微信截图_20230423164322.png


完毕