利用反射和工厂模式来模拟Hibernate生成对象

User类 Admin类 Member类
package com.tianxiao.whut.domain;
import java.sql.Date;
import java.util.*;

import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

 
@XmlType(name = "User")  
public abstract class User {
	private String userName;
	private String userPassword;
	private String mobile;
	private String email;
	private String nickname;
	private String region;
	private java.util.Date birthday ;
	private String telephone;
	private String address;
	private String postcode;
	private String signature;
	private String imageUrl;
	private int userID;
	

}



package com.tianxiao.whut.domain;

public class Admin extends User {

	@Override
	public boolean getIsAdmin() {
		// TODO Auto-generated method stub
		return true;
	}
}



package com.tianxiao.whut.domain;

public class Member extends User {

	@Override
	public boolean getIsAdmin() {
		// TODO Auto-generated method stub
		return false;
	}
}


WebContent/WEB-INF/reflection.xml 如下

<?xml version="1.0" encoding="UTF-8"?>
<beans>

<bean id="admin" class="com.tianxiao.whut.domain.Admin">
</bean>

<bean id="member" class="com.tianxiao.whut.domain.Member">
</bean>

</beans>


实例创建类如下

package com.tianxiao.whut.factory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.tianxiao.whut.domain.User;

public class InstanceFactory {
	private Map<String,String> map;
	
	
	public InstanceFactory(String fileName) {
		String dirPath = System.getProperty("user.dir");;
		
		map= readXml(dirPath+fileName);
		
	}
	
	public Object getBean(String className) {
		if(!map.containsKey(className)) 
			return null;
		Class c = null;
		Object o = null;
		try {
			c = Class.forName(map.get(className));
			o = c.newInstance();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return o;
			
		
	}
	
	public Map<String,String> readXml(String fileName) {
			Map map= new HashMap<String,String>();
			DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
			try {
				   DocumentBuilder dombuilder=domfac.newDocumentBuilder();
				   InputStream is=new FileInputStream(fileName);
				   Document doc=dombuilder.parse(is);
				   Element root=doc.getDocumentElement();
				   NodeList dbinfo=root.getElementsByTagName("bean");
				   if(dbinfo!=null) {
					   for(int i=0;i<dbinfo.getLength();i++){
						  Node db=dbinfo.item(i);
						  NamedNodeMap attr = db.getAttributes();
						  Node id = attr.getNamedItem("id");
						  Node classname = attr.getNamedItem("class");
						  map.put(id.getNodeValue(), classname.getNodeValue());
				       }        
				     }
				  } catch (ParserConfigurationException e) {
				   // TODO Auto-generated catch block
				   e.printStackTrace();
				  } catch (FileNotFoundException e) {
				   // TODO Auto-generated catch block
				   e.printStackTrace();
				  } catch (SAXException e) {
				   // TODO Auto-generated catch block
				   e.printStackTrace();
				  } catch (IOException e) {
				   // TODO Auto-generated catch block
				   e.printStackTrace();
				  }
			return map;
			
		  }
}


Junit测试类如下:

import org.junit.Test;

import com.tianxiao.whut.domain.User;

public class InstanceFactoryTest {

	@Test
	public void testGetBean() {
		//fail("Not yet implemented");
		InstanceFactory instanceFactory = new InstanceFactory("/WebContent/WEB-INF/reflection.xml");
		User user = (User)instanceFactory.getBean("admin");
		System.out.println(user.getIsAdmin());
		user = (User)instanceFactory.getBean("member");
		System.out.println(user.getIsAdmin());
		
		
		
	}

}


这样就可以通过对象名来生成对象了

你可能感兴趣的:(利用反射和工厂模式来模拟Hibernate生成对象)