Design Pattern-facade (appearance Manager)

Source: Internet
Author: User
Design Pattern-facade (appearance Manager)

Banqiao Renren http://www.jdon.com 2002/4/6/

Click here to attend monthly design model lectures

Definition of Facade Mode: Provides a consistent interface for a group of interfaces in the subsystem.

A typical application of facade is the application of database JDBC. The following is an example of database operations:

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.exe cutequery ();
If (rset. Next ()){
System. Out. println (rset. getstring ("<column name "));
}
} Catch (sexception e ){
E. printstacktrace ();
} Finally {
Rset. Close ();
Prep. Close ();
Conn. Close ();
}
}

The preceding example shows the most common methods for operating databases in JSP.

In applications, database operations are often required.CodeIt must be troublesome. You need to extract the unchanged parts into an interface, which introduces the facade appearance object. if we change the class later. the <driver> In forname is also very convenient. For example, you only need to replace the driver in the facade interface from the MySQL database to the Oracle database.

We made a facade interface, using this interface, in the above exampleProgramYou can change it 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.exe cutequery ();
If (rset. Next ()){
System. Out. println (rset. getstring ("<column name "));
}
} Catch (sexception e ){
E. printstacktrace ();
} Finally {
MySQL. Close ();
MySQL = NULL;
}
}

It is very simple to see. All programs use the modified interface for database access, reducing system complexity and increasing flexibility.

If you want to use the connection pool, you only need to modify it for the facade interface.

 

We can see that facade is actually a common method to streamline the relationship between systems and reduce the coupling between systems. Maybe you are using it without knowing it is facade.

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.