Spring项目简单工厂模式例子,根据不同参数获得不同的Service实例

avatar 2019年06月13日13:30:39 6 9559 views
博主分享免费Java教学视频,B站账号:Java刘哥
之前看了段代码,关于简单工厂模式的,用到了反射,但是里面代码比较乱,不够清晰。这里写一个简单一点的工厂模式。 1.接口
  1. package com.example.demo.service;
  2. public interface SystemService {
  3.     String sayHello();
  4. }
  2.多个实现
  1. package com.example.demo.service.impl;
  2. import com.example.demo.service.SystemService;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class WindowsServiceImpl implements SystemService {
  6.     @Override
  7.     public String sayHello() {
  8.         return "大家好,我是Windows系统!";
  9.     }
  10. }
 
  1. package com.example.demo.service.impl;
  2. import com.example.demo.service.SystemService;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class LinuxServiceImpl implements SystemService {
  6.     @Override
  7.     public String sayHello() {
  8.         return "大家好,我是Linux系统!";
  9.     }
  10. }
 
  1. package com.example.demo.service.impl;
  2. import com.example.demo.service.SystemService;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class MacOsServiceImpl implements SystemService {
  6.     @Override
  7.     public String sayHello() {
  8.         return "大家好,我是macOS系统!";
  9.     }
  10. }
  3.工厂类
  1. package com.example.demo.service;
  2. import java.util.Map;
  3. public class SystemServiceFactory {
  4.     /**
  5.      * Map中的Value是 ServiceBean
  6.      */
  7.     private Map<String, SystemService> serviceMap;
  8.     /**
  9.      * 返回对应的 Service
  10.      *
  11.      * @param platform
  12.      * @return
  13.      */
  14.     public SystemService createSystemService(String platform) {
  15.         return serviceMap.get(platform);
  16.     }
  17.     public void setServiceMap(Map<String, SystemService> serviceMap) {
  18.         this.serviceMap = serviceMap;
  19.     }
  20. }
  4.配置类 实例化工厂类
  1. package com.example.demo.config;
  2. import com.example.demo.service.SystemService;
  3. import com.example.demo.service.SystemServiceFactory;
  4. import com.example.demo.service.impl.MacOsServiceImpl;
  5. import com.example.demo.service.impl.WindowsServiceImpl;
  6. import com.example.demo.service.impl.LinuxServiceImpl;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import javax.annotation.Resource;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. @Configuration
  13. public class SystemConfig {
  14.     @Resource
  15.     private WindowsServiceImpl windowsService;
  16.     @Resource
  17.     private LinuxServiceImpl linuxService;
  18.     @Resource
  19.     private MacOsServiceImpl macOsService;
  20.     @Bean
  21.     public SystemServiceFactory createFactory () {
  22.         SystemServiceFactory factory = new SystemServiceFactory();
  23.         Map<String, SystemService> serviceMap = new HashMap<>();
  24.         serviceMap.put("windows",windowsService);
  25.         serviceMap.put("linux",linuxService);
  26.         serviceMap.put("macOS",macOsService);
  27.         factory.setServiceMap(serviceMap);
  28.         return factory;
  29.     }
  30. }
  5.测试类 注入工厂类,根据入参不同创建不同的产品(Service)
  1. package com.example.demo.controller;
  2. import com.example.demo.service.SystemService;
  3. import com.example.demo.service.SystemServiceFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class MainController {
  9.     @Autowired
  10.     private SystemServiceFactory systemServiceFactory;
  11.     @GetMapping("/hello")
  12.     public String hello() {
  13. //        String platform = "Windows 10";
  14. //        String platform = "Mac OS X 10_12_6";
  15.         String platform = System.getProperties().getProperty("os.name");
  16.         SystemService systemService = null;
  17.         if (platform.toLowerCase().indexOf("windows") != -1) {
  18.             systemService = systemServiceFactory.createSystemService("windows");
  19.         } else if (platform.toLowerCase().indexOf("linux") != -1) {
  20.             systemService = systemServiceFactory.createSystemService("linux");
  21.         } else if (platform.toLowerCase().indexOf("mac") != -1) {
  22.             systemService = systemServiceFactory.createSystemService("macOS");
  23.         } else {
  24.             return "系统不支持,不能向您问好!";
  25.         }
  26.         return systemService.sayHello();
  27.     }
  28. }
 
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0