The definition of a skin pattern (façade): Provides a consistent interface for a set of interfaces in a subsystem.
The façade a typical application is the application of database JDBC, as in the following case to the operation of the database:
Copy Code code as follows:
public class Dbcompare {
Connection conn = null;
PreparedStatement prep = NULL;
ResultSet rset = null;
try {
Class.forName ("<driver>"). newinstance ();
conn = Drivermanager.getconnection ("<database>");
String sql = "SELECT * from <table> WHERE <column name> =?";
PREP = conn.preparestatement (SQL);
Prep.setstring (1, "<column value>");
RSet = Prep.executequery ();
if (Rset.next ()) {
System.out.println (rset.getstring ("<column name"));
}
catch (Sexception e) {
E.printstacktrace ();
finally {
Rset.close ();
Prep.close ();
Conn.close ();
}
}
The above example is the most common method of database operation in JSP.
In the application, often need to operate on the database, each time write a piece of code must be more trouble, you need to refine the part of the unchanged, made an interface, which introduced a façade appearance object. It is also very convenient to replace the <driver> in Class.forName, for example, from the MySQL database to the Oracle database, as long as the driver in the façade interface can be changed.
We made a façade interface, and using that interface, the program in the example above can be changed as follows:
Copy Code code as follows:
public class Dbcompare {
String sql = "SELECT * from <table> WHERE <column name> =?";
try {
MySQL msql=new mysql (sql);
Prep.setstring (1, "<column value>");
RSet = Prep.executequery ();
if (Rset.next ()) {
System.out.println (rset.getstring ("<column name"));
}
catch (Sexception e) {
E.printstacktrace ();
finally {
Mysql.close ();
Mysql=null;
}
}
Visible is very simple, all programs to database access is to use a modified interface, reduce the complexity of the system, increased flexibility.
If we want to use a connection pool, we can just modify it for the façade interface.
As can be seen from the above picture, the façade is actually a way to rationalize the relationship between systems, reducing the coupling between systems is a common method, you may have been unknowingly used, although it is not known that it is a façade.