Application of interface-oriented programming in Java Web Three-tier architecture and the use of Factory mode to solve problems __ios

Source: Internet
Author: User
Tags java web

Application of interface-oriented programming in Java Web Three-tier architecture and problem solving using Factory mode

Why programming to interface:

In the application, we are generally interface-oriented programming, which is conducive to the expansion of the system and porting. Like the three-tier architecture in the Java Web: The presentation layer, the business logic layer, the data persistence layer, the upper level is called the underlying interface. Because once the lower level implementation changes: for example, the persistence layer from database operations to file operations, the upper tier because of calls to the underlying interface without changing any code, facilitate system expansion and migration.

The main points of the design of interface-oriented programming:

For example, to add a user now, the data persistence layer interface looks like this:

Package Kane; Import java.util.List; Import Kane. UserInfo; /** * Userinfodao Layer interface * @author Kane/public interface Userinfodao {//Add registered user basic information public void Adduserinfo (UserInfo u);}

And the two implementations are as follows:

First, the database implementation code:

Package Kane; Import java.sql.Connection; Import java.sql.PreparedStatement; Import Java.sql.ResultSet; Import java.sql.SQLException; Import java.util.ArrayList; Import java.util.List; Import Kane. UserInfo; Import Kane. dbutility; /** * Userinfodao Interface Implementation class * @author Kane */public class Userinfodao4sql implements Userinfodao {public void Adduserinfo Rinfo u) {Connection conn = null; PreparedStatement pstmt = null; try {conn = dbutility.getconnection (); Conn.setautocommit (false); String sql = "INSERT into User_info (Id,username,password) VALUES (?,?,?)"; pstmt = conn.preparestatement (sql); Pstmt.setint (1, U.getid); Pstmt.setstring (2, U.getusername ()); Pstmt.setstring (3, U.getpassword ()); Pstmt.execute (); Conn.commit (); catch (SQLException e) {try {conn.rollback ();} catch (SQLException E1) {e1.printstacktrace ();} e.printstacktrace (); finally {dbutility.closepreparedstatement (pstmt); Dbutility.closeconnection (); } } }

Second, the file implementation class code is as follows:

Package Kane; Import java.; Import java.util.ArrayList; Import java.util.List; Import Kane. UserInfo; /** * Userinfodao Interface Implementation class * @author Kane */public class Userinfodao4file implements Userinfodao {public void Adduserinfo (Us Erinfo u) {//File Operation implementation code ...}}

The business Logic layer code looks like this:

Package Kane; Import java.sql.Connection; Import java.util.List; Import Kane. Userinfodao; Import Kane. UserInfo; /** * Userinfomanage Business logic * * @author Kane */public class Userinfomanage {private Userinfodao Userinfodao = null; publi C Userinfomanage () {Userinfodao = new Userinfodao4sql ();}/** public Userinfomanage () {Userinfodao = new UserInfoDao4 File (); Userinfodao = (Userinfodao) interfacefactory.getinstance (). Getbean (Userinfodao.class); } */public void Adduserinfo (UserInfo u) {userinfodao.adduserinfo (U);}}

Interface-oriented programming issues:

This way, if you want to save the user from replacing the database with a file, modify the Userinfodao = new Userinfodao4sql () in the constructor method, and this line of code has the principle of not conforming to the modification extension, because once you change the implementation, you change the code.

The solution to the problem of interface-oriented programming:

The specific approach is to extract the changed code into the configuration file, just like the database configuration file, so you can modify the configuration file directly without modifying the code. Here we introduce a method called the Factory mode method, the code is as follows:

 package Kane; Import Java.util.HashMap; Import Java.util.Map; Import org.dom4j.Document; Import org.dom4j.DocumentException; Import org.dom4j.Element; Import Org.dom4j.io.SAXReader; /** * Extract the same method of creating interface objects--using interfacefactory to create each interface product * @author Kane uses a single case mode */public class Interfacefactory {private static Interfacefactory instance = null; Private document document = NULL; Private map<string, object> beanmap = new hashmap<string, object> (); Private interfacefactory () {try {document = new Saxreader (). Read (Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Interfacefactory.xml")); catch (Documentexception e) {e.printstacktrace ();}} public static Interfacefactory getinstance () {if (instance = null) {instance = new Interfacefactory ();} return Instanc E /** * If the bean is in the map, it is returned directly, otherwise the bean is created and added to the map * to make the load more efficient * @param ID * @return The bean to be obtained/@SuppressWarnings ("uncheck Ed ") Public synchronized object Getbean (Class c) {object bean = null; bean = Beanmap.get (C.getname ()); if (bean = = null) {try {element e = (Element) document. SelectObject ("//interface[@id =/" "+ c.getname () +"/"]); String className = E.attributevalue ("class"); Bean = Class.forName (className). newinstance (); Beanmap.put (C.getname (), Bean); catch (Instantiationexception E1) {e1.printstacktrace ();} catch (Illegalaccessexception E2) {e2.printstacktrace ();} catch (ClassNotFoundException E3) {e3.printstacktrace ();}} return bean; } }

The configuration file Interfacefactory.xml code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <bean> <interface id= "Kane. Userinfodao "Class=" Kane. Userinfodao4sql "/> </bean>

The class Interfacefactory uses DOM4J to parse the XML file and then uses the reflection mechanism Class.getname () in the Getbean () method to generate the interface implementation class to be used. and put the implementation class in the map so that you can use it directly from the map to improve efficiency. The configuration file has a database implementation, and if you want to use a file implementation, convert the profile to:

<?xml version= "1.0" encoding= "UTF-8"?> <bean> <interface id= "Kane. Userinfodao "Class=" Kane. Userinfodao4file "/> </bean>

The code for the business logic layer is modified as follows so that the configuration file can be written without modifying the code:

Package Kane; Import java.sql.Connection; Import java.util.List; Import Kane. Userinfodao; Import Kane. UserInfo; Import Kane. Interfacefactory; /** * Userinfomanage Business logic * * @author Kane */public class Userinfomanage {private Userinfodao Userinfodao = null; publi C Userinfomanage () {Userinfodao = (Userinfodao) interfacefactory.getinstance (). Getbean (Userinfodao.class);} public Vo ID adduserinfo (UserInfo u) {userinfodao.adduserinfo (U);}}

Related Article

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.