之前看了段代码,关于简单工厂模式的,用到了反射,但是里面代码比较乱,不够清晰。这里写一个简单一点的工厂模式。
1.接口
2.多个实现
3.工厂类
4.配置类 实例化工厂类
5.测试类 注入工厂类,根据入参不同创建不同的产品(Service)
1.接口
- package com.example.demo.service;
- public interface SystemService {
- String sayHello();
- }
2.多个实现
- package com.example.demo.service.impl;
- import com.example.demo.service.SystemService;
- import org.springframework.stereotype.Service;
- @Service
- public class WindowsServiceImpl implements SystemService {
- @Override
- public String sayHello() {
- return "大家好,我是Windows系统!";
- }
- }
- package com.example.demo.service.impl;
- import com.example.demo.service.SystemService;
- import org.springframework.stereotype.Service;
- @Service
- public class LinuxServiceImpl implements SystemService {
- @Override
- public String sayHello() {
- return "大家好,我是Linux系统!";
- }
- }
- package com.example.demo.service.impl;
- import com.example.demo.service.SystemService;
- import org.springframework.stereotype.Service;
- @Service
- public class MacOsServiceImpl implements SystemService {
- @Override
- public String sayHello() {
- return "大家好,我是macOS系统!";
- }
- }
3.工厂类
- package com.example.demo.service;
- import java.util.Map;
- public class SystemServiceFactory {
- /**
- * Map中的Value是 ServiceBean
- */
- private Map<String, SystemService> serviceMap;
- /**
- * 返回对应的 Service
- *
- * @param platform
- * @return
- */
- public SystemService createSystemService(String platform) {
- return serviceMap.get(platform);
- }
- public void setServiceMap(Map<String, SystemService> serviceMap) {
- this.serviceMap = serviceMap;
- }
- }
4.配置类 实例化工厂类
- package com.example.demo.config;
- import com.example.demo.service.SystemService;
- import com.example.demo.service.SystemServiceFactory;
- import com.example.demo.service.impl.MacOsServiceImpl;
- import com.example.demo.service.impl.WindowsServiceImpl;
- import com.example.demo.service.impl.LinuxServiceImpl;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import javax.annotation.Resource;
- import java.util.HashMap;
- import java.util.Map;
- @Configuration
- public class SystemConfig {
- @Resource
- private WindowsServiceImpl windowsService;
- @Resource
- private LinuxServiceImpl linuxService;
- @Resource
- private MacOsServiceImpl macOsService;
- @Bean
- public SystemServiceFactory createFactory () {
- SystemServiceFactory factory = new SystemServiceFactory();
- Map<String, SystemService> serviceMap = new HashMap<>();
- serviceMap.put("windows",windowsService);
- serviceMap.put("linux",linuxService);
- serviceMap.put("macOS",macOsService);
- factory.setServiceMap(serviceMap);
- return factory;
- }
- }
5.测试类 注入工厂类,根据入参不同创建不同的产品(Service)
- package com.example.demo.controller;
- import com.example.demo.service.SystemService;
- import com.example.demo.service.SystemServiceFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class MainController {
- @Autowired
- private SystemServiceFactory systemServiceFactory;
- @GetMapping("/hello")
- public String hello() {
- // String platform = "Windows 10";
- // String platform = "Mac OS X 10_12_6";
- String platform = System.getProperties().getProperty("os.name");
- SystemService systemService = null;
- if (platform.toLowerCase().indexOf("windows") != -1) {
- systemService = systemServiceFactory.createSystemService("windows");
- } else if (platform.toLowerCase().indexOf("linux") != -1) {
- systemService = systemServiceFactory.createSystemService("linux");
- } else if (platform.toLowerCase().indexOf("mac") != -1) {
- systemService = systemServiceFactory.createSystemService("macOS");
- } else {
- return "系统不支持,不能向您问好!";
- }
- return systemService.sayHello();
- }
- }
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏