zoukankan      html  css  js  c++  java
  • (设计模式)简单工厂模式之通过配置文件动态创建实现类

    通常我们在使用简单工厂模式的时候会由创建方法create通过传入的参数来判断要实例化哪个对象,就像下面这样:

    1. public static class ImageSelectFactory {
    2. public static IImageSelect createIImageSelect(ImageSelectClientMode mode) {
    3. IImageSelect imageSelect = null;
    4. if (mode == ImageSelectClientMode.COLLECTION_IMAGE) {
    5. imageSelect = new CollectionImage();
    6. } else if (mode == ImageSelectClientMode.LOCAL_PHOTO) {
    7. imageSelect = new LocalPhoto();
    8. } else if (mode == ImageSelectClientMode.WORKS_IMAGE) {
    9. imageSelect = new WorksImage();
    10. } else if (mode == ImageSelectClientMode.TAKE_PHOTO) {
    11. imageSelect = new TakePhoto();
    12. } else if (mode == ImageSelectClientMode.SUPER_IMAGE_LIB) {
    13. imageSelect = new SuperImageLib();
    14. }
    15. return imageSelect;
    16. }
    17. }
    这里面定义了5个IImageSelect接口的子类,通过定义好的泛型ImageSelectClientMode来决定实例化哪个子类,现在遇到这么一个问题,如果添加到第6个子类的话,那就必须要更改ImageSelectFactory类以及枚举ImageSelectClientMode,可能你会说“改一下又何妨?”,虽不说影响不影响什么开闭设计原则,但是有个情况你可成想到,你这个类要打包发布给别人用呢?别人在没有源码的情况下如何扩展呢?这里就需要我们动态的通过配置文件来加载实现类了。

    实现的基本思路为:通过读取本地的.properties文件来获取我们需要实例化的类,然后通过反射来生成对象,这样当你把发布出去的时候,使用者只用更改配置文件就可以让工厂去实例化自己后来才写的实现类,我们看看实现方式:

    ImageSelectClient.properties:
    
    1. COLLECTION_IMAGE=com.kongfuzi.student.support.bitmap.select.CollectionImage
    2. LOCAL_PHOTO=com.kongfuzi.student.support.bitmap.select.LocalPhoto
    3. WORKS_IMAGE=com.kongfuzi.student.support.bitmap.select.WorksImage
    4. TAKE_PHOTO=com.kongfuzi.student.support.bitmap.select.TakePhoto
    5. SUPER_IMAGE_LIB=com.kongfuzi.student.support.bitmap.select.SuperImageLib


    1. public static class ImageSelectFactory {
    2. public static IImageSelect createIImageSelect(String type) {
    3. IImageSelect mIImageSelect;
    4. //实例化Properties对象,用于读取.properties
    5. Properties properties = new Properties();
    6. InputStream is = null;
    7. try {
    8. is = ImageSelectClient.class.getResourceAsStream("ImageSelectClient.properties");
    9. //装载ImageSelectClient.properties文件
    10. properties.load(is);
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. } finally {
    14. try {
    15. is.close();
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. try {
    21. //根据key获取value,value即为将要实例化的类
    22. Class<?> aClass = Class.forName(properties.getProperty(type));
    23. //使用反射进行实例化
    24. mIImageSelect = (IImageSelect) aClass.newInstance();
    25. } catch (ClassNotFoundException e) {
    26. e.printStackTrace();
    27. } catch (InstantiationException e) {
    28. e.printStackTrace();
    29. } catch (IllegalAccessException e) {
    30. e.printStackTrace();
    31. }
    32. return mIImageSelect;
    33. }
    34. }

    这样,我们就可以随便实现子类,然后在.properties文件中添加对应的包路径,然后通过ImageSelectFactory就可以进行实例化了。



  • 相关阅读:
    几个容易混淆的集合类
    ajax操作时用于提高用户体验的两段备用代码
    word-wrap和word-break的区别
    清除MAC OS X上的流氓软件
    Windows Azure IP地址详解
    实现跨云应用——基于DNS的负载均衡
    Windows Azure虚拟机和云服务实例计费方式更新
    证明你是你——快速开启Windows Azure多重身份验证
    Windows 10 L2TP 809错误
    新版Microsoft Azure Web管理控制台
  • 原文地址:https://www.cnblogs.com/nangonghui/p/13381624.html
Copyright © 2011-2022 走看看