For the appearance of the model, in the project development and practical application is very frequent, but it is very easy to understand, the following is a brief introduction.
First, Concept introduction
appearance mode (facade), he hides the complexity of the system and provides the client with an interface that can access the system. This type of design pattern belongs to the structural model . Provides a unified access interface for a set of interfaces in a subsystem that makes it easier to access or use the subsystem.
Ii. Roles and usage scenarios
In short, the pattern is to encapsulate some complex processes into an interface for easier use by external users. In this mode, design to 3 characters.
1). Façade role : The core of the appearance pattern. It is called by the client role and is familiar with the functions of the subsystem. A combination of several functions is scheduled internally based on the needs of the customer role.
2). subsystem role : realizes the function of subsystem. It is unknown to the customer role and facade. It can have internal interaction within the system or an interface that can be called by the outside world.
3). Customer role : Complete the functionality to be implemented by calling Facede.
Usage scenarios:
1-modules that provide external access to complex modules or subsystems;
2-subsystems are independent of each other;
3-In a chromatography structure, you can use the appearance pattern to define the entry for each layer of the system.
Third, examples
Below, we will implement this pattern with a simple example.
Each computer has CPU, Memory, Disk. When the computer is turned on and off, the corresponding parts are also turned on and off, so that the appearance mode is used to decouple the user from the part. Such as:
The creation of the package body:
Code implementation
The first is the Subsystem class:
"Code Listing--1"
1 PackageCom.huawei.facadeDesign.children;2 3 ImportOrg.apache.log4j.Logger;4 5 /**6 * CPU Subsystem class7 * @authorAdministrator8 *9 */Ten Public classCPU One { A Public Static FinalLogger Logger = Logger.getlogger (CPU.class); - Public voidStart () - { theLogger.info ("CPU is start ..."); - } - - Public voidShutDown () + { -Logger.info ("CPU is ShutDown ..."); + } A}
"Code Listing--2"
1 PackageCom.huawei.facadeDesign.children;2 3 ImportOrg.apache.log4j.Logger;4 5 /**6 * Disk Subsystem class7 * @authorAdministrator8 *9 */Ten Public classDisk One { A Public Static FinalLogger Logger = Logger.getlogger (Disk.class); - Public voidStart () - { theLogger.info ("Disk is start ..."); - } - - Public voidShutDown () + { -Logger.info ("Disk is ShutDown ..."); + } A}
"Code Listing--3"
1 PackageCom.huawei.facadeDesign.children;2 3 ImportOrg.apache.log4j.Logger;4 5 /**6 * Memory Subsystem class7 * @authorAdministrator8 *9 */Ten Public classMemory One { A Public Static FinalLogger Logger = Logger.getlogger (Memory.class); - Public voidStart () - { theLogger.info ("Memory is start ..."); - } - - Public voidShutDown () + { -Logger.info ("Memory is ShutDown ..."); + } A}
And then, the façade class facade
"Code Listing--4"
1 PackageCom.huawei.facadeDesign.facade;2 3 ImportOrg.apache.log4j.Logger;4 5 ImportCom.huawei.facadeDesign.children.CPU;6 ImportCom.huawei.facadeDesign.children.Disk;7 Importcom.huawei.facadeDesign.children.Memory;8 9 Ten /** One * Façade class (CORE) A * @authorAdministrator - * - */ the Public classComputer - { - Public Static FinalLogger Logger = Logger.getlogger (computer.class); - + PrivateCPU CPU; - Privatememory Memory; + Privatedisk disk; A Publiccomputer () at { -CPU =NewCPU (); -Memory =NewMemory (); -Disk =NewDisk (); - } - Public voidStart () in { -Logger.info ("Computer Start Begin"); to Cpu.start (); + Disk.start (); - Memory.start (); theLogger.info ("Computer Start End"); * } $ Panax Notoginseng Public voidShutDown () - { theLogger.info ("Computer ShutDown begin"); + Cpu.shutdown (); A Disk.shutdown (); the Memory.shutdown (); +Logger.info ("Computer ShutDown end ..."); - } $}
Finally, the customer role.
"Code Listing--5"
1 Packagecom.huawei.facadeDesign;2 3 ImportOrg.apache.log4j.Logger;4 5 ImportCom.huawei.facadeDesign.facade.Computer;6 7 /**8 * Client Class9 * @authorAdministratorTen * One */ A Public classcilent { - Public Static FinalLogger Logger = Logger.getlogger (cilent.class); - Public Static voidMain (string[] args) the { -Computer computer =Newcomputer (); - Computer.start (); -Logger.info ("================="); + Computer.shutdown (); - } + A}
"Code Listing--6" Run results
From the above example, with this facade class, that is, the computer class, the user does not have to call the subsystem in person Disk,memory, CPU class, do not need to know the implementation details of the system, even do not have to know the structure of the internal system. The client only needs to interact with facade.
Iv. Advantages-Loose coupling
The decoupling between client and subsystem makes it easier to extend and maintain the module function inside the subsystem;
-Easy to use
The client does not need to know the implementation within the subsystem at all, or does not need to know the internal structure of the subsystem, it only needs to interact with the facade class.
-Better partitioning of access levels
Some methods are external to the system, and some methods are used within the system to interact with each other. Subsystems put those exposed to the external functions into the façade, so that the client can be used, it is good to hide the internal details of the subsystem.
V. Extension and application (supplementary)
...
Hope that everyone's study is helpful, please give more advice.
Appearance mode in Java design mode (façade mode)