Facade in design pattern (appearance)

xiaoxiao2021-04-05  259

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

Facade A typical application is the application of database JDBC, as follows of the following example:

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 ("

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 definitely more troublesome. You need to extract the unmatched part of it, make an interface, which introduces a Facade look. If we replace the Class. in Forname is also very convenient, such as replacing the Oracle database from the MySQL database, as long as the Driver in the Facade interface can be replaced.

We have made a FACADE 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 very simple, all programs are used to access databases, reducing the complexity of the system, increasing 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-132062.html

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