Java implementation of structural design patterns-appearance patterns

Source: Internet
Author: User

Java implementation of structural design patterns-appearance patterns

I. Description

The appearance mode is also called the facade mode, which is to package a complex system. The external interfaces of the system are provided by the appearance class. When a complex system needs to provide external interfaces, it needs to encapsulate the interfaces provided externally in a uniform appearance class for external systems. The most important feature of appearance mode is to package fine-grained objects into coarse-grained objects. Applications can call fine-grained objects by accessing this appearance object. In this way, the application can only see the appearance object, but not the specific details object. This will undoubtedly reduce the complexity of the application and improve the maintainability of the program.

In general, the appearance mode is a set of interfaces provided for the subsystem. This set of interfaces provides a unified interface, allowing other systems to access the system through this unified interface, this mode consists of three parts: the abstract appearance class, the specific appearance class that implements the abstract appearance class, and other sub-system classes.

 

 

Ii. Advantages and Disadvantages of appearance Mode

Advantage: The appearance mode provides a unified external interface to avoid direct connections between external systems and subsystems, thus reducing the dependencies and complexity between systems. The appearance mode encapsulates complex fine-grained object services into simple and easy-to-use coarse-grained function services, facilitating client calls (such as java API calls ).

Disadvantage: a restricted external system can call subsystems only according to the methods provided in the appearance class.

 

Iii. Source Code

In this case, java uses jdbc to connect to the database and perform data query and update operations. The query result set is output and all resources in the database are released, an abstract appearance class defines the access interface to the external system. Two specific appearance classes implement the abstract appearance class and provide a unified access interface to the outside world, this facilitates client access.

1. Sub-system class

Package tong. day5_2.facade; import java. io. file; import java. io. fileInputStream; import java. util. properties;/*** load Config from the current project's main directory. properties Database Configuration File, initialize database configuration information */public class ConfitgInfo {private static String driver; private static String url; private static String user; private static String password; public void setConfig () {try {Properties properties = new Properties (); File file = new File (System. ge TProperty ("user. dir ") +" \ Config. properties "); properties. load (new FileInputStream (file); driver = properties. getProperty ("DatabaseDriver"); url = properties. getProperty ("jdbcUrl"); user = properties. getProperty ("DatabaseUser"); password = properties. getProperty ("DatabasePassword"); System. out. println ("driver:" + driver + ", url:" + url + ", user:" + user + ", password:" + password);} catch (Exception e) {e. printStack Trace (); System. out. println ("configuration information initialization failed! ") ;}} Public static String getDriver () {return driver;} public static void setDriver (String driver) {ConfitgInfo. driver = driver;} public static String getUrl () {return url;} public static void setUrl (String url) {ConfitgInfo. url = url;} public static String getUser () {return user;} public static void setUser (String user) {ConfitgInfo. user = user;} public static String getPassword () {return password;} public static void setPassword (String password) {ConfitgInfo. password = password ;}}

Package tong. day5_2.facade; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement;/*** all functions related to database Connection and Operations * @ author tong **/public class DBConnection {public connection Connection; public Statement statement; public ResultSet resultset; /*** create a database connection * @ throws Exception */public DBConnection () throws Exception {Class. forName (ConfitgInfo. getDriver (); connection = DriverManager. getConnection (ConfitgInfo. getUrl (), ConfitgInfo. getUser (), ConfitgInfo. getPassword (); statement = connection. createStatement ();}/*** query database operations ** @ param SQL SELECT statement * @ return ResultSet object * @ throws Exception */public ResultSet select (String SQL) throws Exception {resultset = statement.exe cuteQuery (SQL); return resultset;} public ResultSet select (String table, String limit, int min, int max) throws SQLException {String SQL = "SELECT * FROM (SELECT. *, rownum rn from "+ table +" a where "+ limit +") where rn <= "+ max +" and rn> = "+ min; resultset = statement.exe cuteQuery (SQL); return resultset;}/***** update database operations ** @ param SQL INSERT, DELETE, UPDATE statement * @ throws Exception */public void update (String SQL) throws Exception {statement.exe cute (SQL);} public Connection getConnection () {return connection ;} public void setConnection (Connection connection) {this. connection = connection;} public Statement getStatement () {return statement;} public void setStatement (Statement statement) {this. statement = statement;} public ResultSet getResultset () {return resultset;} public void setResultset (ResultSet resultset) {this. resultset = resultset ;}}

Package tong. day5_2.facade; import java. SQL. resultSet; import java. SQL. SQLException;/*** print the output ResultSet result set * @ author tong **/public class PrintResult {public void printResultSet (ResultSet resultset) {try {if (resultset. next () {String username = resultset. getString ("username"); String passwd = resultset. getString ("passwd"); System. out. println ("username:" + username + ", password:" + passwd) ;}} catch (SQLException e) {e. printStackTrace ();}}}
Package tong. day5_2.facade; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement;/*** release all database resources * @ author tong **/public class ReleaseDB {// close database resources in sequence: resultset, statement, connection public void close (Connection connection, Statement statement, ResultSet resultset) {try {if (resultset! = Null) {resultset. close ();} if (statement! = Null) {statement. close ();} if (connection! = Null) {connection. close () ;}catch (SQLException e) {e. printStackTrace ();}}}
2. Abstract appearance

 

Package tong. day5_2.facade; // abstract appearance class, which provides a unified interface for external systems and simplifies the calling of public interface Facade {public abstract void dbOperation (String querySql) by external systems );}

3. Specific appearance

 

Package tong. day5_2.facade; import java. SQL. resultSet;/*** the specific appearance of the database query operation, mainly responsible for executing the select query statement, print the result set * @ author tong */public class DBSelectFacade implements Facade {private ConfitgInfo confitgInfo; private DBConnection dbConnection; private PrintResult printResult; private ReleaseDB releaseDB; public partition () {confitgInfo = new ConfitgInfo (); try {// obtain the database connection and statementdbConnection = new DBConnection ();} catch (Exception e) {e. printStackTrace () ;}printresult = new printResult (); releaseDB = new ReleaseDB () ;}// call other business methods, provide the client with the simplest interface public void dbOperation (String querySql) {// load the database configuration file to initialize the database configuration information confitgInfo. setConfig (); // query the data result ResultSet resultSet = null; try {resultSet = dbConnection. select (querySql);} catch (Exception e) {e. printStackTrace ();} // output data result set printResult. printResultSet (resultSet); // release all database resources releaseDB. close (dbConnection. getConnection (), dbConnection. getStatement (), dbConnection. getResultset ());}}

Package tong. day5_2.facade; import java. SQL. resultSet;/*** the specific appearance class of the database update operation, mainly responsible for executing INSERT, DELETE, UPDATE statement * @ author tong */public class DBUpdateFacade implements Facade {private ConfitgInfo confitgInfo; private DBConnection dbConnection; private ReleaseDB releaseDB; public response () {confitgInfo = new ConfitgInfo (); try {// get the database connection and statementdbConnection = new DBConnection ();} catch (Exception e) {e. printStackTrace () ;}releasedb = new releaseDB () ;}// call other business methods to provide the client with the simplest interface public void dbOperation (String querySql) {// load the database configuration file to initialize the database configuration information confitgInfo. setConfig (); // execute INSERT, DELETE, UPDATE statement try {dbConnection. update (querySql);} catch (Exception e) {e. printStackTrace ();} // release all database resources releaseDB. close (dbConnection. getConnection (), dbConnection. getStatement (), dbConnection. getResultset ());}}
4. Client call

 

Package tong. day5_2.facade;/*** the client test class can operate the database simply by calling the appearance mode class. The appearance design mode shields users from a series of operations on the database. * The biggest feature of the appearance mode is to encapsulate fine-grained objects into coarse-grained objects. Applications can call fine-grained objects by accessing this appearance object. * Through the appearance packaging, the application can only see the appearance object, but not the specific details object, which will undoubtedly reduce the complexity of the application, it also improves the maintainability of the Program * @ author tong **/public class FacadeTest {/*** @ param args */public static void main (String [] args) {String sqlString = "select username, password from user"; String updateString = "update user set username = 'hangsan' where username = 'lisi'"; DBSelectFacade dBuseFacade = null ;; DBUpdateFacade dbUpdateFacade = null; try {// call the query database appearance class object to query the data dBuseFacade = new DBSelectFacade (); dBuseFacade. dbOperation (sqlString); // call the update database appearance class object to update the data dbUpdateFacade = new DBUpdateFacade (); dbUpdateFacade. dbOperation (updateString);} catch (Exception e1) {e1.printStackTrace ();}}}

 

Iv. Config. properties configuration file required for database Initialization

 

jdbcDriver=oracle.jdbc.driver.OracleDriverjdbcUrl=jdbc:oracle:thin:@192.168.1.230:1521:orclDatabaseUser=systemDatabasePassword=tiger




 

 

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.