码迷,mamicode.com
首页 > 其他好文 > 详细

反射+工厂模型+Properties

时间:2015-08-12 18:42:29      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

通过Properties类从文件中读取配置信息,从而可以避免大量的代码修改。

1、新建fruit.properties(文件名任意,路径在项目默认目录下),并写入内容:

1 apple=properties.Apple
2 banana=properties.Banana

 

2、编写Fruit接口:

1 public interface Fruit {
2     void eat();
3 }

 

3、Fruit接口的实现类:

public class Banana implements Fruit{

    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println("Banana!");
    }
    
}
1 public class Apple implements Fruit{
2 
3     @Override
4     public void eat() {
5         // TODO Auto-generated method stub
6         System.out.println("Apple!");
7     }
8 
9 }

 

4、FruitFactory工厂:

 1 public class FruitFactory {
 2     public static Fruit getFruitInstance(String className) {
 3         Fruit fruit = null;
 4         try {
 5             fruit = (Fruit) Class.forName(className).newInstance();
 6         } catch (Exception e) {
 7             // TODO Auto-generated catch block
 8             e.printStackTrace();
 9         }
10         return fruit;
11     }
12 }

 

5、主类:

 1 public class ProperDemo {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         // FruitFactory.getFruitInstance(Apple.class.getName()).eat();
 6         Properties pro = init();
 7         // Properties根据键值对获取信息
 8         Fruit f = FruitFactory.getFruitInstance(pro.getProperty("banana"));
 9         f.eat();
10         System.out.println(pro.getProperty("iapple"));
11     }
12 
13     static Properties init() {
14         // 新建Properties对象
15         Properties pro = new Properties();
16         File file = new File("fruit.properties");
17         if (file.exists()) {
18             try {
19                 pro.load(new FileInputStream(file));
20                 pro.setProperty("iapple", "properties.iApple");
21             } catch (Exception e) {
22                 // TODO Auto-generated catch block
23                 e.printStackTrace();
24             }
25         } else {
26             pro.setProperty("apple", "properties.Apple");
27             // pro.loadFromXML(new FileInputStream(""));
28         }
29         return pro;
30     }
31 }

 

运行结果:

Banana!
properties.iApple

 

可以用Properties#store(InputStream in,String key)将数据写入指定文件中。用Properties#clear()清除文件中所有数据。

 

通过Properties获取JVM配置信息:

1 Properties pro= System.getProperties();
2 pro.list(System.out);

以键值对形式显示Properties文件中所有配置信息:

 1 void showDetail() {
 2         Properties pro = System.getProperties();
 3         pro.list(System.out);
 4         Enumeration<?> enu = pro.propertyNames();
 5         while (enu.hasMoreElements()) {
 6             String key = (String) enu.nextElement();
 7             String value = pro.getProperty(key);
 8             System.out.println("key:" + key + "  value:" + value);
 9         }
10     }

 

java中Properties类的操作:http://www.cnblogs.com/bakari/p/3562244.html

java读取Properties文件的六种方法:http://blog.csdn.net/Senton/article/details/4083127

反射+工厂模型+Properties

标签:

原文地址:http://www.cnblogs.com/mada0/p/4724718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!