Use "Factory mode + reflection + configuration file" for decoupling and

Source: Internet
Author: User

When we use Java's three-tier architecture development, if we create the service layer directly with the Web layer, and then create the DAO layer directly with the service layer, then if we change the underlying implementation class, we are going to modify the source code, which is obviously not quite in line with the development requirements. How should the problem be solved? Yes, we can reduce the coupling by taking advantage of the "Factory mode + Reflection + Profile" mode, which allows us to achieve our requirements simply by modifying the configuration file.

Below we first through the form of the case to analyze the traditional mode, factory model deficiencies.

In this we first set a few classes, it is not in the following one by one write, you want to according to these several categories to see the content of the later Austrian.

The interface of DAO layer Userdao the Implementation class of DAO layer Userdaoimp

Beanfactory Class for Factory class

Traditional way:

To create a DAO layer implementation class object

Userdao Userdao = new Userdaoimp;

In the traditional way, if the implementation of the class object changes due to changes in demand, we need to change the original implementation class object to a new implementation class object, which is obviously not in line with our OCP principle, and the need to modify the place if very many, then the workload is very large.

(The OCP principle explains: The extension is open to the program, and the code is closed for modification.) )

Factory mode:

We reduce the coupling between the interface and the implementation class through a factory class (beanfactory) so that we can see less effort to modify the code. As shown in figure:



In this way, we need to change the implementation of the class when the change, we just have to modify the factory class in the implementation class object, but this still needs to modify the source code, fault tolerance is still relatively high, how can not modify the source?

Factory mode + Reflection + configuration file to complete the program decoupling and:

Here we need to use DOM4J to parse the XML file so we need to lead two jar packages:

The file name of the XML file is Applicationcontext.xml

The contents of the XML file name are as follows:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"?>
<beans>
	<bean id= "Userdao" class= " Store.dao.impl.UserDaoImpl "></bean>
	<bean id=" Productdao "class=" Store.dao.impl.ProductDaoImpl " ></bean>
	<bean id= "Orderdao" class= "Store.dao.impl.OrderDaoImpl" ></bean>
</beans ></span>

Code in the Beanfactory class:

<span style= "font-family:kaiti_gb2312;font-size:18px;"
>import org.dom4j.Document;
Import org.dom4j.Element;

Import Org.dom4j.io.SAXReader; public class Beanfactory {//parent interface is passed as an ID as an argument to implement calling different implementation class public static Object Getbean (String id) {try {//create sax
			Reader object to read the configuration file Saxreader reader = new Saxreader (); Reads the contents of the configuration file, obtains the Document Object document document = Reader.read (BeanFactory.class.getClassLoader (). getResourceAsStream ("
			Applicationcontext.xml "));
			Use XPath to read to class element beanelement = (Element) Document.selectsinglenode ("//bean[@id = '" +id+ "']");
			Gets the path to the class after String value = Beanelement.attributevalue ("class");
			System.out.println (value);
			Using reflection to obtain the implementation class object class Clazz = Class.forName (value);
		return Clazz.newinstance ();
		} catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
	} return null; }} </span> 
We do not need to modify the source code in this form, but we can achieve our requirements by modifying the contents of the tags <bean></bean> in the configuration file, thus reducing the fault tolerance rate.





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.