java学习之路---设计模式----工厂代理模式的进一步研究

来源:互联网 发布:fc2最新域名fc2cncom 编辑:程序博客网 时间:2024/06/06 14:15
原来的工厂模式:http://blog.csdn.net/javastudyr/article/details/16811969
可以看看工厂模式的基本实现之后才来看这个结合了反射和配置文件的工厂模式

这里的工厂模式是结合了反射,原来的普通的工厂模式,每增加一个子类,都要修改工厂类,但是用了反射就不需要了。。

          package fstest;

interface Fruit{
     public void eat();
}


class Apple implements Fruit{

     public void eat() {
          System. out.println("吃苹果" );
          
     }
     
}

class Orange implements Fruit{

     public void eat() {
     System.out.println( "吃橘子");
          
     }
     
}
class  Factory{
     
     public static Fruit getInstance(String className){
          Fruit fruit= null;
          
           try {
               fruit=(Fruit)Class. forName(className).newInstance();
          } catch (InstantiationException e) {
              
              e.printStackTrace();
          } catch (IllegalAccessException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (ClassNotFoundException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
           return fruit;
     }
}


public class FactoryDemo1 {
     
     public static void main(String[] args) {
          Fruit f=Factory. getInstance("fstest.Apple");//直接传入包名加类名就可以了
           if(f!=null ){
              f.eat();
          }
     }

}

2.但是这样有出现了一个问题,用户不知道可以使用多少个子类,所以我们在这里在加上配置文件就好了

      package fstest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

interface Fruit{
     public void eat();
}


class Apple implements Fruit{

     public void eat() {
          System.out.println("吃苹果");
         
     }
    
}

class Orange implements Fruit{

     public void eat() {
     System.out.println("吃橘子");
         
     }
    
}
class  Factory{
    
     public static Fruit getInstance(String className){
          Fruit fruit=null;
         
          try {
               fruit=(Fruit)Class.forName(className).newInstance();
          } catch (InstantiationException e) {
              
               e.printStackTrace();
          } catch (IllegalAccessException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (ClassNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
         
          return fruit;
     }
}

class Init{
     public static Properties getPro(){
          Properties pro=new Properties();
         
          File f=new File("d:\\fruit.properties");
          if(f.exists()){
               try {
                    pro.load(new FileInputStream(f));
               } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
          }else{
               pro.setProperty("apple", "fstest.Apple");
               pro.setProperty("orange", "fstest.Orange");
               try {
                    pro.store(new FileOutputStream(f), "FRUIT CLASS");
               } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
          }
         
          return pro;
     }
}



public class FactoryDemo1 {
    
     public static void main(String[] args) {
          Properties pro=Init.getPro();
          Fruit f=Factory.getInstance(pro.getProperty("apple"));
          if(f!=null){
               f.eat();
          }
     }

}


结果:
吃苹果