• 反射机制:工厂设计模式


    工厂设计模式最大好处是可以在应用中解耦合操作,

    传统工厂模式:

    package 类集;
    interface Fruit{
        public void eat() ;    // 吃水果
    }
    class Apple implements Fruit{
        public void eat(){            // 覆写eat()方法
            System.out.println("** 吃苹果");
        }
    };
    class Orange implements Fruit{
        public void eat(){
            System.out.println("** 吃橘子") ;
        }
    };
    class Factory01{
        public static Fruit getInstance(String className){
            Fruit fruit = null ;
            if("apple".equals(className))
                fruit=new Apple();
            if("orange".equals(className))
                fruit=new Orange();
            return fruit ;
        }
    };
    public class Factory {
    
        public static void main(String[] args) {
             
            Fruit f = Factory01.getInstance("apple") ;
            if(f!=null){
                f.eat() ;
            }
        }
    
    }

    输出结果:

    吃苹果

    以上代码存在问题,如果扩充子类,那么需要修改工厂类。如果要想扩充子类,不修改工厂,则必须使用反射机制完成

    改进后成为反射形式。

    代码:

    package 类集;
    interface Fruit{
        public void eat() ;    // 吃水果
    }
    class Apple implements Fruit{
        public void eat(){            // 覆写eat()方法
            System.out.println("** 吃苹果");
        }
    };
    class Orange implements Fruit{
        public void eat(){
            System.out.println("** 吃橘子") ;
        }
    };
    class Factory01{
        public static Fruit getInstance(String className){
            Fruit fruit = null ;
            try{
                fruit = (Fruit)Class.forName(className).newInstance() ;
            }catch(Exception e){
                e.printStackTrace() ;
            }
            return fruit ;
        }
    };
    public class Factory {
    
        public static void main(String[] args) {
             
            Fruit f = Factory01.getInstance("类集.Apple") ;
            if(f!=null){
                f.eat() ;
            }
        }
    
    }

    输出结果:

    ** 吃苹果

    以上确实在扩充子类时候不用扩充工厂类,但是程序代码依然存在问题,如果使用时候,

    使用了完整的“包.类名称”,肯定很麻烦,所以,此时,可以通过配置文件的方式保存这些完整的类路径

    程序代码:

    在属性文件里是这样写的:

    apple=类集.Apple
    orange=类集.Orange

    程序运行的时候就可以将属性文件的内容读取出来,之后直接操作属性文件中的key,就可以避免输入过长的类路径

    代码

    package 类集;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.Properties;
    
    interface Fruit{
        public void eat() ;    // 吃水果
    }
    class Apple implements Fruit{
        public void eat(){            // 覆写eat()方法
            System.out.println("** 吃苹果");
        }
    };
    class Orange implements Fruit{
        public void eat(){
            System.out.println("** 吃橘子") ;
        }
    };
    class Init{
        public static Properties getPro(){
            Properties pro = new Properties() ;
            File f = new File("d:\fruit.properties") ;    // 找到属性文件
            try{
                if(f.exists()){    // 文件存在
                    pro.load(new FileInputStream(f)) ;    // 读取属性
                }else{
                    pro.setProperty("apple","类集.Apple") ;  //如果不存在该文件,就自己设置几个属性
                    pro.setProperty("orange","类集.Orange") ;
                    pro.store(new FileOutputStream(f),"FRUIT CLASS") ;  //保存设置的属性,把设置的属性存储到该文件夹中。
                }
            }catch(Exception e){}
            return pro ;
        }
    };
    class Factory1{
        public static Fruit getInstance(String className){
            Fruit fruit = null ;
            try{
                fruit = (Fruit)Class.forName(className).newInstance() ;
            }catch(Exception e){
                e.printStackTrace() ;
            }
            return fruit ;
        }
    };
    public class Factory {
    
        public static void main(String[] args) {
             
            Properties pro = Init.getPro() ;  //初始化属性
            Fruit f = Factory1.getInstance(pro.getProperty("apple")) ;  //通过key得到所需属性的对应值
            if(f!=null){
                f.eat() ;
            }
        }
    
    }

    输出结果:

    ** 吃苹果

    以上程序达到了配置文件与程序代码想分离目的,这种设计思路是以后开发基本思路。最新设计理念,是程序中直接通过注释方式进行配置。

  • 相关阅读:
    教你彻底弄懂JS中this的指向
    js-原型,原型链
    Firefox SyntaxError: invalid regexp group ChunkLoadError: Loading chunk task-show-task-show-module failed.
    什么是标签语义化?标签语义化有什么意义?
    什么是事件委托?jquery和js怎么去实现?
    express框架
    es6
    node搭建服务器
    node内容
    ajax面试题
  • 原文地址:https://www.cnblogs.com/alsf/p/6624831.html
Copyright © 2020-2023  润新知