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.