自己实现一个Factory
2006-09-09 20:11
435 查看
自己实现一个Factory
CollectionFactory.java文件:
package cn.itcast;
import java.util.*;
import java.io.*;
public class CollectionFactory
{
public static String KEY_PROPERTIES_FILE = "cn.itcast.collectionfile";
public static String KEY_CLASS = "class";
public static String KEY_ELEMENTS = "elements";
public static Collection getCollection() throws Exception
{
String properties_file = System.getProperty(KEY_PROPERTIES_FILE);
if(properties_file == null)
{
throw new IllegalArgumentException("not found property file!");
}
InputStream ips = CollectionFactory.class.getResourceAsStream("/" + properties_file);
if(ips == null)
{
throw new IllegalArgumentException("file not exists!");
}
Properties props = new Properties();
props.load(ips);
ips.close();
String strClassName = props.getProperty(KEY_CLASS);
Class classCollection = Class.forName(strClassName);
Collection objCollection = (Collection) classCollection.newInstance();
String strElements = props.getProperty(KEY_ELEMENTS);
String [] elements = strElements.split(",");
for(String element : elements)
{
objCollection.add(element);
}
return objCollection;
}
}
MainClass.java文件:
package cn.itcast;
import java.util.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
public class MainClass
{
public static void main(String [] args) throws Exception
{
Log log = LogFactory.getLog(MainClass.class);
Collection objCollection = CollectionFactory.getCollection();
for(Object obj : objCollection)
{
log.info(obj.toString());
}
}
}
CollectionFactory.java文件:
package cn.itcast;
import java.util.*;
import java.io.*;
public class CollectionFactory
{
public static String KEY_PROPERTIES_FILE = "cn.itcast.collectionfile";
public static String KEY_CLASS = "class";
public static String KEY_ELEMENTS = "elements";
public static Collection getCollection() throws Exception
{
String properties_file = System.getProperty(KEY_PROPERTIES_FILE);
if(properties_file == null)
{
throw new IllegalArgumentException("not found property file!");
}
InputStream ips = CollectionFactory.class.getResourceAsStream("/" + properties_file);
if(ips == null)
{
throw new IllegalArgumentException("file not exists!");
}
Properties props = new Properties();
props.load(ips);
ips.close();
String strClassName = props.getProperty(KEY_CLASS);
Class classCollection = Class.forName(strClassName);
Collection objCollection = (Collection) classCollection.newInstance();
String strElements = props.getProperty(KEY_ELEMENTS);
String [] elements = strElements.split(",");
for(String element : elements)
{
objCollection.add(element);
}
return objCollection;
}
}
MainClass.java文件:
package cn.itcast;
import java.util.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
public class MainClass
{
public static void main(String [] args) throws Exception
{
Log log = LogFactory.getLog(MainClass.class);
Collection objCollection = CollectionFactory.getCollection();
for(Object obj : objCollection)
{
log.info(obj.toString());
}
}
}
相关文章推荐
- 自己写的一个单例实现
- cocos2d-x自己实现的一个button类
- 各位大牛帮忙看看问题出来哪?自己实现一个数组模板类,编译无法通过
- 贴一个好多年前自己写的AVL实现
- 自己实现一个右滑删除的ListView
- 根据数组的原理,自己实现一个简易版的ArrayList
- 如何自己实现一个可自定义业务流程步骤的工作流
- 百度地图 实现行走轨迹描绘(觉得网上好多缺点,不全,自己写了一个)
- 自己实现的一个简易Spring框架(IoC+AOP)
- 记一个自己写nodejs实现的树洞
- 自己实现一个泛型ArrayList
- 实现一个自己的服务器监控器
- windows sdk编程系列文章 ---- 利用APC实现向一个运行中的进程注入自己的代码
- Java事务处理全解析(四)—— 成功的案例(自己实现一个线程安全的TransactionManager)
- Lucene系列三:Lucene分词器详解、实现自己的一个分词器
- 赛码网,简单计算,用C语言自己实现的一个程序,仅供参考
- 自己实现一个bubble_sort(冒泡排序),可以完成不同类型数据的排序
- C++自己实现一个简单地Vector
- 根据源码自己实现一个容器的空间适配器
- 基于Json.NET自己实现MVC中的JsonValueProviderFactory