Overview:
Providing a consistent interface for a set of interfaces in a subsystem, the facade schema defines a high-level interface that makes this subsystem easier to use.
Type: Structural mode.
Class Diagram:
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 the subsystem, but it also brings some usability difficulties to users who do not need to customize the subsystem.
Facade can provide a simple default view that is sufficient for most users, and those who need more customization can cross the facade layer.
2. There is a large dependency between the client program and the implementation part of the abstract class. The introduction of facade separates this subsystem from customers and other subsystems, which can improve the independence and portability of subsystems.
3. When you need to build a hierarchical subsystem, use the facade pattern to define the entry points for each layer in the subsystem.
If subsystems are interdependent, you can make them communicate only through facade, simplifying their dependencies.
Participants:
1.Facade
Know which subsystem classes are responsible for processing requests.
The client's request is proxied to the appropriate subsystem object.
2.Subsystemclasses
Implement the functions of the subsystem.
Handles tasks assigned by the facade object.
There is no relevant information for facade; that is, there is no pointer to facade.
Example:
FaçadePublicClassfacade {ServiceA sa; SERVICEB SB; SERVICEC SC;PublicFacade () {sa =New Serviceaimpl (); SB =New Servicebimpl (); sc =New Servicecimpl (); }PublicvoidMethodA () {Sa.methoda (); Sb.methodb ();}PublicvoidMethodB () {sb.methodb (); Sc.methodc ();}PublicvoidMethodc () {sc.methodc (); Sa.methoda ();}} SubsystemclassesPublicClassServiceaimplImplementsServiceA {PublicvoidMethodA () {System.out.println ("This is Service a"); }}PublicClassServicebimplImplementsSERVICEB {PublicvoidMethodB () {System.out.println ("This is Service B"); }}PublicClassServicecimplimplements SERVICEC {public void methodc () {System.out.println (public class test {public static void main (string[] args) {servicea sa = new Serviceaimpl (); SERVICEB sb = new Servicebimpl (); Sa.methoda (); Sb.methodb (); System.out.println ( "========"); //facade facade facade = new facade (); Facade.methodA (); Facade.methodb (); }}
Result
这是服务A这是服务B========这是服务A这是服务B这是服务B这是服务C
23 Design Modes (21): Appearance mode