转载请注明出处:http://blog.csdn.net/l1028386804/article/details/45568655
I. 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. Second, 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 subsystems more reusable and easier to customize for subsystems, but it also brings some usability difficulties for users who do not need a custom subsystem. The 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 a façade separates the subsystem from the customer and other subsystems, which can improve the subsystem's independence and portability.
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. Third, 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. four, class diagram
v. Examples
Façade
Package com.lyz.design.facade;
/** *
Façade
* @author Liuyazhuang
* * *
/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
Package com.lyz.design.facade;
/** * subsystemclasses * @author Liuyazhuang * */public
class Serviceaimpl implements ServiceA {public
void MethodA () {
System.out.println ("This is Service a");
}
Package com.lyz.design.facade;
/**
* subsystemclasses * @author Liuyazhuang * */public
class Servicebimpl implements SERVICEB { c6/>public void MethodB () {
System.out.println ("This is Service B");
}
Package com.lyz.design.facade;
/** * subsystemclasses * @author Liuyazhuang * */public
class Servicecimpl implements SERVICEC {public
void Methodc () {
System.out.println ("This is Service C");
}
Package com.lyz.design.facade;
/**
* Test
* @author Liuyazhuang
* * *
/public class Test {public
static void main (string[ ] {
servicea sa = new Serviceaimpl () args);
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