The definition of a skin pattern (façade): Provides a consistent interface for a set of interfaces in a subsystem.
Façade a typical application is the application of database JDBC, the following example of the operation of the database: 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 example above is the most common approach to database operations 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 have made a façade interface, using this interface, the program in the example above can be changed 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.
Appearance mode
Overview
Provides a consistent interface for a set of interfaces in a subsystem, and the façade pattern defines a high-level interface that makes the subsystem easier to use.
Applicability
1. When you want to provide a simple interface for a complex subsystem. Subsystems tend to become more complex as they evolve
. Most patterns will produce more and smaller classes when used. This makes the subsystem more reusable and easier to
customize to the sub system, but it also brings some usability difficulties for users who do not need a custom subsystem.
a façade can provide a simple default view, which is sufficient for most users, and those who need
more customization can cross the façade layer.
2. There is a large dependency between the client program and the implementation part of the abstract class. The introduction of the façade separates this subsystem from the guest
and other subsystems, which can improve the independence and portability of the subsystem.
3. When you need to build a hierarchical subsystem, use the façade pattern to define the entry points for each layer in the subsystem.
if subsystems are interdependent, you can simplify their dependencies by allowing them to communicate only through a façade
.
participants
1.Facade
knows which subsystem classes are responsible for processing requests.
delegate the client's request to the appropriate subsystem object.
2.Subsystemclasses
realizes the subsystem function.
handles tasks assigned by a façade object.
there is no relevant information about the façade; there is no pointer to the façade.
class Diagram
Example
Façade
public class Façade {
servicea sa;
SERVICEB sb;
SERVICEC SC;
Public façade () {
sa = new Serviceaimpl ();
SB = new Servicebimpl ();
sc = new Servicecimpl ();
}
public void MethodA () {
Sa.methoda ();
Sb.methodb ();
}
public void MethodB () {
sb.methodb ();
SC.METHODC ();
}
public void Methodc () {
sc.methodc ();
Sa.methoda ();
}
subsystemclasses
public class Serviceaimpl implements ServiceA {public
void MethodA () {
System.out.println ("This is Service a");
}
}
public class Servicebimpl implements SERVICEB {public
void MethodB () {
System.out.println ("This is Service B");
}
}
public class Servicecimpl implements SERVICEC {public
void Methodc () {
System.out.println ("This is Service C");
}
}
Test
public class Test {public
static void Main (string[] args) {
servicea sa = new Serviceaimpl ();
SERVICEB sb = new Servicebimpl ();
Sa.methoda ();
Sb.methodb ();
System.out.println ("========");
Façade façade
= new façade ();
Facade.methoda ();
Facade.methodb ();
}
Result
This is service a
this is service B ======== This is service a This is service B this is service
b
This is service C