zoukankan      html  css  js  c++  java
  • 工厂模式+反射+泛型

    接口

    public interface IService {
        String getName();
    }
    

    实现类

    • UserService
    package com.icodesoft.service;
    public class UserService implements IService {
        @Override
        public String getName() {
            return "User Service";
        }
    }
    
    • ProductService
    package com.icodesoft.service;
    public class ProductService implements IService {
        @Override
        public String getName() {
            return "Product Service";
        }
    }
    

    工厂类

    • FactoryService
    public class FactoryService {
    
        private FactoryService(){}
    
        // 普通写法
        public static IService getService(String key) {
            if ("user-service".equals(key)) {
                return new UserService();
            } else if ("product-service".equals(key)) {
                return new ProductService();
            } else {
                return null;
            }
        }
    
        // 稍作改进
        public static IService getInstance(String className) {
            try {
                Class<?> clzz = Class.forName(className);
                IService instanceService = (IService) clzz.newInstance();
                return instanceService;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    • Factory
    public class Factory {
        private Factory() {}
    
        // 推荐写法
        public static <T> T getInstance(String className) {
            try {
                Class<?> clzz2 = Class.forName(className);
                T instanceService = (T) clzz2.newInstance();
                return instanceService;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    

    测试类

    • DemoApplication
    import com.icodesoft.service.Factory;
    import com.icodesoft.service.FactoryService;
    import com.icodesoft.service.IService;
    
    public class DemoApplication {
        public static void main(String[] args) {
            String key = "user-service";
            IService userService = FactoryService.getService(key );
            System.out.println("**************FactoryService.getService************** Service Name: " + userService.getName());
            key = "product-service";
            IService productService = FactoryService.getService("product-service");
            System.out.println("**************FactoryService.getService************** Service Name: " + productService.getName());
    
            String className = "com.icodesoft.service.UserService"; // 从配置文件中获取
            userService = FactoryService.getInstance(className);
            System.out.println("**************FactoryService.getInstance************** Service Name: " + userService.getName());
    
            userService = Factory.<IService>getInstance("com.icodesoft.service.UserService");
            System.out.println("**************Factory.<IService>getInstance************** Service Name: " + userService.getName());
    
            className = "com.icodesoft.service.ProductService"; // 从配置文件中获取
            productService = FactoryService.getInstance(className);
            System.out.println("**************FactoryService.getInstance************** Service Name: " + productService.getName());
    
            productService = Factory.<IService>getInstance(className );
            System.out.println("**************Factory.<IService>getInstance************** Service Name: " + productService.getName());
        }
    }
    
  • 相关阅读:
    util-tool工具 、时间格式化
    好用的 UI 调试技巧
    扩展jQuery的方法
    mybatis判断是否传递了条件
    mysql创建视图不允许子查询
    springMVC传递MAP给JS
    XPS15 安装ubuntu之后无法进入系统
    XPS15 9560 切换显卡之后,无法登陆的解决方式
    fedora安装chrome报错
    linux挂载硬盘
  • 原文地址:https://www.cnblogs.com/bjxly/p/13738443.html
Copyright © 2011-2022 走看看