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());
        }
    }
    
  • 相关阅读:
    Android 上传图片到服务器 okhttp一
    Kotlin 扩展——省略findViewById
    音频的播放一
    layui+ztree 树状下拉框
    Element里el-badge在el-tab里视图不被渲染问题
    linux之cat 操作
    cmd命令行中查看、修改、删除与添加环境变量
    cmd 文件/文件夹的一切操作
    操作
    11. 判断是给属性前加typeof 可以同时判断属性是否存在
  • 原文地址:https://www.cnblogs.com/bjxly/p/13738443.html
Copyright © 2011-2022 走看看