Java implements Abstract Factory + reflection by reading configuration files through dom4j

Source: Internet
Author: User

The main principle of reflection implementation in donet is to configure the differences in the config configuration file, and use the interface to find the specific implementation name under the bin path of u to run the program.

In Java, you can also configure different configurations in the configuration file like donet. In addition, we adopt the following method to achieve more convenient.

Logical description:

Now we want to add the interface layer to layer B and layer D and use the factory. We can regard the creation of B and D as two series, and then we can use the abstract factory to create.

Profile: beans-config.xml. Service-class and Dao-class correspond to two series of products respectively. In the sub-menu, the ID corresponds to the interface namespace, and the class corresponds to the namespace of the implementation class.

<?xml version="1.0" encoding="UTF-8"?><beans> <service-class> <service id="com.xxjstgb.drp.basedata.manager.ItemManager" class="com.xxjstgb.drp.basedata.manager.ItemManagerImpl" /> <service id="com.xxjstgb.drp.flowcard.manager.FlowCardManager" class="com.xxjstgb.drp.flowcard.manager.impl.FlowCardManagerImpl" /> </service-class> <dao-class> <dao id="com.xxjstgb.drp.basedata.dao.ItemDao" class="com.xxjstgb.drp.basedata.dao.ItemDao4OracleImpl" /> <dao id="com.xxjstgb.drp.flowcard.dao.FlowCardDao" class="com.xxjstgb.drp.flowcard.dao.impl.FlowCardDaoImpl" /> </dao-class></beans>

Abstract Factory: beanfactory. Read the configuration file, obtain the related objects, and save the created objects in map.

Package COM. xxjstgb. DRP. util; import Java. util. hashmap; import Java. util. map; // dom4jimport Org. dom4j. document; import Org. dom4j. extends entexception; 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 create two series of products * 1, Manager series * 2, Dao series products * @ author liuzhengquan **/public class beanfactory {Private Static beanfactory instance = new beanfactory (); // default system configuration file name private final string beansconfigfile = "beans-config.xml"; // Save the Dao-Related Object private document DOC; /** key = ID value in the configuration file * value = corresponds to the ID object */private map servicemap = new hashmap (); // Save the service-related object private map daomap = new hashmap (); // Save the Dao-Related Object private beanfactory () {try {Doc = new saxreader (). read (thread. currentthread (). getcontextclassloader (). getresourceasstream (beansconfigfile);} catch (incluentexception e) {e. printstacktrace (); throw new runtimeexception () ;}} public static beanfactory getinstance () {return instance ;} /*** get the service product series according to the product number * @ Param serviceid * @ return */Public synchronized object getserviceobject (Class C) {// If an instance of the relevant object exists, returns 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 (); // place the created object in the map servicemap. put (C. getname (), service);} catch (exception e) {Throw new runtimeexception ();} return service ;} /*** obtain Dao series products by product number * @ Param daoid * @ return */Public synchronized object getdaoobject (Class C) {// If an instance of the relevant object exists, returns 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 (); // place the created object in 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) beanfactory. getinstance (). getserviceobject (itemmanager. class); system. out. println ("itemmanager" + itemmanager); itemdao = (itemdao) beanfactory. getinstance (). getdaoobject (itemdao. class); system. out. println ("itemdao:" + itemdao); flowcardmanager = (flowcardmanager) beanfactory. getinstance (). getserviceobject (flowcardmanager. class); // flowcardmanager = new flowcardmanagerimpl (); system. out. println (flowcardmanager); flowcarddao = (flowcarddao) beanfactory. getinstance (). getdaoobject (flowcarddao. class); // flowcarddao = new flowcarddaoimpl (); system. out. println ("flowcarddao:" + flowcarddao );}}

Running result:

Summary:

Through the implementation of abstract factory + reflection, the call layer can only recognize interfaces without dealing with specific implementations to achieve understanding coupling. At the same time, because the configuration file contains interfaces and implemented namespaces, we can use corresponding interfaces. class Attribute, click the namespace, and configure the configuration file ID and class in the form of key-value pairs in map for reflection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.