Facade in design pattern (appearance)

zhaozj2021-02-16  54

FACADE definition: Provide a consistent interface for a set of interfaces in the subsystem.

FACADE A typical application is the application of the database JDBC, such as the operation of the database:

public class DBCompare {Connection conn = null; PreparedStatement prep = null; ResultSet rset = null; try {Class.forName ( "") .newInstance (); conn = DriverManager.getConnection ( ""); String SQL = "SELECT * from

where =?"; preip = conn.preparestatement (SQL); prep.setstring (1, ""); Rset = preip.executeQuery (); if (RSET.NEXT ()) {system.out.println (RSET.GETSTRING ("

The above example is the most common way to operate in JSP.

In the application, it is often necessary to operate the database. Every time you write the above code, it is certainly more troublesome, you need to refine the unmatched part of it, make a interface, which introduces the FACADE appearance item. If we replace it in the of Class.Forname, we are also very convenient, such as from the MySQL Database, as long as the driver in the Facade interface is replaced.

We have made a interface, using this interface, the program in the previous example can be changed as follows:

Public class dbcompare {string SQL = "SELECT * from

where =?"; try {mysql msql = new mysql (sql); prep.setstring (1, ""); Rset = Prep.executeQuery (); if (Rset.Next ()) {system.out.println (RSET.GETSTRING ("

It can be seen that all programs are used to use the remedies to reduce the complexity of the system and increase flexibility.

If we want to use the connection pool, you can modify the FACADE interface.

As can be seen from the figure, FACADE is actually a common way to reduce system relationships, reducing a common method of inter-system coupling, maybe you are not known, although I don't know it is facade.

转载请注明原文地址:https://www.9cbs.com/read-20614.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.048, SQL: 9