Fork me on GitHub

工厂模式+反射+泛型

接口

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());
    }
}
posted @ 2020-09-27 11:30  逍遥メ风  阅读(362)  评论(0编辑  收藏  举报