java語言實現結構型設計模式—面板模式

來源:互聯網
上載者:User

標籤:面板模式   java原始碼   結構型設計模式   facade   jdbc資料庫連接   

一、描述

面板模式又叫門面模式,就是對一個複雜的系統進行封裝,該系統對外的介面統一由外觀類提供。當一個複雜的系統需要對外提供介面時,就需要將對外提供的介面統一封裝在一個外觀類中供外系統使用。面板模式最大的特點就是將細粒度的對象封裝成粗粒度的對象,應用程式通過訪問這個外觀對象來完成細粒度對象的調用。這樣應用程式只能看到外觀對象,而不會看到具體的細節對象,這樣無疑會降低應用程式的複雜度,並且提高了程式的可維護性。

總的來說,面板模式就是為子系統對外提供的一組介面,這組介面提供一個統一的介面,使得其它系統對該系統的訪問都通過這個統一的介面來完成,該模式主要由3個部分組成:抽象外觀類、實現抽象外觀類的具體外觀類和其它子系統類。


二、面板模式的優缺點

優點:面板模式通過提供一個統一的對外介面,避免了外部系統和子系統之間的直接聯絡,從而降低了系統間的依賴和複雜度。面板模式將複雜的細粒度的物件服務封裝成簡單的易使用的粗粒度的功能服務,方便了用戶端的調用(例如java的API調用)。

缺點:限制類外部系統對子系統調用的靈活性,只能按照外觀類中提供的方法對子系統進行調用。


三、原始碼

本案例類比java使用jdbc串連資料庫,並進行資料查詢和更新操作,輸出查詢結果集、釋放資料庫中的所有資源這幾個子系統,我們通過一個抽象外觀類定義了對外部系統的提供者,並由兩個具體的外觀類實現了抽象外觀類,對外提供一個統一的提供者,從而方便用戶端的訪問。

1、子系統類

package tong.day5_2.facade;import java.io.File;import java.io.FileInputStream;import java.util.Properties;/** * 從當前項目主目錄下載入Config.properties資料庫設定檔,初始化資料庫配置資訊 */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.getProperty("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.printStackTrace();System.out.println("配置資訊初始化失敗!"); }}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;/** * 有關資料庫連接和操作的所有功能 * @author tong * */public class DBConnection {    public Connection connection;    public Statement statement;    public ResultSet resultset;    /**     * 建立資料庫連接     * @throws Exception     */    public DBConnection() throws Exception {    Class.forName(ConfitgInfo.getDriver());        connection = DriverManager.getConnection(ConfitgInfo.getUrl(), ConfitgInfo.getUser(), ConfitgInfo.getPassword());        statement = connection.createStatement();    }            /**     * 查詢資料庫操作     *     * @param sql SELECT語句     * @return ResultSet 對象     * @throws Exception     */    public ResultSet select(String sql) throws Exception {               resultset = statement.executeQuery(sql);        return resultset;    }    public ResultSet select(String table, String limit, int min, int max) throws SQLException {               String sql = "SELECT * FROM (SELECT A.* , ROWNUM RN FROM " + table + " A WHERE " + limit + " ) WHERE RN <= " + max + " AND RN >= " + min;        resultset = statement.executeQuery(sql);        return resultset;    }           /**     * 更新資料庫操作     *     * @param sql INSERT ,DELETE,UPDATE 語句     * @throws Exception     */    public void update(String sql) throws Exception {        statement.execute(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;/** * 列印輸出ResultSet結果集 * @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;/** * 釋放資料庫有關所有資源 * @author tong * */public class ReleaseDB {//依次關閉資料庫資源: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、抽象外觀類

package tong.day5_2.facade;//抽象外觀類,為外界系統提供一個統一的介面,簡化外界系統的調用public interface Facade {public abstract void dbOperation(String querySql);}

3、具體外觀類

package tong.day5_2.facade;import java.sql.ResultSet;/** * 執行資料庫查詢操作的具體外觀類,主要負責執行select查詢語句,並列印結果集 * @author tong * */public class DBSelectFacade implements Facade{private ConfitgInfo confitgInfo;private DBConnection dbConnection;private PrintResult printResult;private ReleaseDB releaseDB;public DBSelectFacade(){confitgInfo = new ConfitgInfo();try {//擷取資料庫連接和statementdbConnection = new DBConnection();} catch (Exception e) {e.printStackTrace();}printResult = new PrintResult();releaseDB = new ReleaseDB();}//調用其它類的業務方法,提供給用戶端一個最簡潔的介面public void dbOperation(String querySql){//載入資料庫設定檔初始化資料庫配置資訊confitgInfo.setConfig();//查詢資料結果ResultSet resultSet = null;;try {resultSet = dbConnection.select(querySql);} catch (Exception e) {e.printStackTrace();}//輸出資料結果集printResult.printResultSet(resultSet);//釋放資料庫的所有資源releaseDB.close(dbConnection.getConnection(), dbConnection.getStatement(), dbConnection.getResultset());}}

package tong.day5_2.facade;import java.sql.ResultSet;/** * 執行資料庫更新操作的具體外觀類,主要負責執行INSERT,DELETE,UPDATE更新語句 * @author tong * */public class DBUpdateFacade implements Facade{private ConfitgInfo confitgInfo;private DBConnection dbConnection;private ReleaseDB releaseDB;public DBUpdateFacade(){confitgInfo = new ConfitgInfo();try {//擷取資料庫連接和statementdbConnection = new DBConnection();} catch (Exception e) {e.printStackTrace();}releaseDB = new ReleaseDB();}//調用其它類的業務方法,提供給用戶端一個最簡潔的介面public void dbOperation(String querySql){//載入資料庫設定檔初始化資料庫配置資訊confitgInfo.setConfig();//執行INSERT ,DELETE,UPDATE 更新語句try {dbConnection.update(querySql);} catch (Exception e) {e.printStackTrace();}//釋放資料庫的所有資源releaseDB.close(dbConnection.getConnection(), dbConnection.getStatement(), dbConnection.getResultset());}}
4、用戶端調用

package tong.day5_2.facade;/** * 用戶端測試類別只要調用面板模式類,就可以簡單操作資料庫,外觀設計模式對使用者屏蔽了資料庫的一系列操作。 * 面板模式最大的特點將細粒度的對象封裝成粗粒度的對象,應用程式通過訪問這個外觀對象,來完成細粒度對象的調用。 * 我們通過外觀的封裝,使應用程式只能看到外觀對象,而不會看到具體的細節對象,這樣無疑會降低應用程式的複雜度,並且提高了程式的可維護性 * @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='zhangsan' where username='lisi'";DBSelectFacade dBuseFacade = null;;DBUpdateFacade dbUpdateFacade = null;try {//調用查詢資料庫的外觀類對象來查詢資料dBuseFacade = new DBSelectFacade();dBuseFacade.dbOperation(sqlString);//調用更新資料庫的外觀類對象來更新資料dbUpdateFacade = new DBUpdateFacade();dbUpdateFacade.dbOperation(updateString);} catch (Exception e1) {e1.printStackTrace();}}}

四、初始化資料庫所需的Config.properties設定檔

<span style="font-size:14px;">jdbcDriver=oracle.jdbc.driver.OracleDriverjdbcUrl=jdbc:oracle:thin:@192.168.1.230:1521:orclDatabaseUser=systemDatabasePassword=tiger</span>





java語言實現結構型設計模式—面板模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.