Logical description:
Now we want to add interface layers to layer B and layer D and use factories. And we can regard the creation of B and the creation of D as two series, and then we can use the abstract factory to create them.
Configuration file: beans-config.xml. service-class and dao-class correspond to two series of products respectively. The id in the submenu corresponds to the namespace of the interface, and the class corresponds to the namespace of the implementation class.
Copy the code code as follows:
[html] view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<service-class>
<service id="com.xxjstgb.drp.basedata.manager.ItemManager" />
<service id="com.xxjstgb.drp.flowcard.manager.FlowCardManager" />
</service-class>
<dao-class>
<dao id="com.xxjstgb.drp.basedata.dao.ItemDao" />
<dao id="com.xxjstgb.drp.flowcard.dao.FlowCardDao" />
</dao-class>
</beans>
Abstract factory : BeanFactory. By reading the configuration file, obtain the relevant objects and save the related created objects in the Map.
Copy the code code as follows:
[java] view plaincopyprint?
package com.xxjstgb.drp.util;
import java.util.HashMap;
import java.util.Map;
//dom4j
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.xxjstgb.drp.basedata.dao.ItemDao;
import com.xxjstgb.drp.basedata.manager.ItemManager;
import com.xxjstgb.drp.flowcard.dao.FlowCardDao;
import com.xxjstgb.drp.flowcard.manager.FlowCardManager;
/**
* Abstract factory, mainly creates two series of products
* 1. Manager series
* 2. Dao series products
* @author liuzhengquan
*
*/
public class BeanFactory {
private static BeanFactory instance=new BeanFactory();
//System default configuration file name
private final String beansConfigFile="beans-config.xml";
//Save Dao related objects
private Document doc;
/*
* key=id value in configuration file
* value=the object corresponding to the Id
*/
private Map serviceMap = new HashMap();//Save Service related objects
private Map daoMap = new HashMap();//Save Dao related objects
private BeanFactory(){
try {
doc=new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream(beansConfigFile));
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
public static BeanFactory getInstance(){
return instance;
}
/**
* Obtain Service series products according to product number
* @param serviceId
* @return
*/
public synchronized Object getServiceObject(Class c){
//If there is a related object instance, return
if(serviceMap.containsKey(c.getName())){
return serviceMap.get(c.getName());
}
Element beanElt=(Element)doc.selectSingleNode("//service[@id=/""+ c.getName() + "/"]");
String className=beanElt.attributeValue("class");
Object service=null;
try {
service=Class.forName(className).newInstance();
//Put the created object into the Map
serviceMap.put(c.getName(), service);
} catch (Exception e) {
throw new RuntimeException();
}
return service;
}
/**
* Obtain Dao series products according to product number
* @param daoId
* @return
*/
public synchronized Object getDaoObject(Class c){
//If there is a related object instance, return
if(daoMap.containsKey(c.getName())){
return daoMap.get(c.getName());
}
Element beanElt=(Element)doc.selectSingleNode("//dao[@id=/""+c.getName()+"/"]");
String className=beanElt.attributeValue("class");
Object dao=null;
try {
dao=Class.forName(className).newInstance();
//Put the created object into the Map
daoMap.put(c.getName(), dao);
} catch (Exception e) {
throw new RuntimeException();
}
return dao;
}
/**
* test
* @param args
*/
public static void main(String[] args){
ItemManager itemManager=(ItemManager)BeanFactory.getInstance().getServiceObject(ItemManager.class);
System.out.println("itemManager"+itemManager);
ItemDao itemDao=(ItemDao)BeanFactory.getInstance().getDaoObject(ItemDao.class);
System.out.println("itemDao:"+itemDao);
FlowCardManager flowCardManager=(FlowCardManager)BeanFactory.getInstance().getServiceObject(FlowCardManager.class);
//FlowCardManager flowCardManager=new FlowCardManagerImpl();
System.out.println(flowCardManager);
FlowCardDao flowCardDao=(FlowCardDao)BeanFactory.getInstance().getDaoObject(FlowCardDao.class);
//FlowCardDao flowCardDao=new FlowCardDaoImpl();
System.out.println("flowCardDao:"+flowCardDao);
}
}