Introduction to the Appearance mode of the Java design pattern (facade mode) _java

Source: Internet
Author: User

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.

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.