首页 > 其他 > 详细

反射机制:工厂设计模式

时间:2017-03-27 00:18:44      阅读:193      评论:0      收藏:0      [点我收藏+]

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

传统工厂模式:

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() ;
        }
    }

}

输出结果:

** 吃苹果

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

反射机制:工厂设计模式

原文:http://www.cnblogs.com/alsf/p/6624831.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!