• 工厂模式+反射+泛型


    接口

    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());
        }
    }
    
  • 相关阅读:
    Angular语法(三)——数据绑定
    Angular语法(二)——模板语法
    Angular语法(一)——展示数据
    Angular常用指令
    windows下启动redis
    WPF实现弹幕
    微信获得用户信息
    拉普拉斯变换
    Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering
    Python之并行
  • 原文地址:https://www.cnblogs.com/bjxly/p/13738443.html
Copyright © 2020-2023  润新知