Overview
When communicating with the internal subsystem, it must be carried out through a unified appearance mode object, which is the appearance mode, also known as the appearance mode. Generally speaking, the Facade pattern is to reduce the dependence between the client and the implementation layer. The purpose of appearance mode is to provide a centralized and simplified communication channel for the subsystem.
UML class diagram
In the UML diagram above, three characters appear:
Client role: The user calls the appearance pattern class through the client to operate the subsystem;
Facade: The client can call this class, which contains the specific functions in the calling subsystem;
Subsystem role (Module): defines specific individual functions in the subsystem;
Code example:
package interview;class ModuleA { public void testA(){ System.out.println("Method in ModuleA"); }}class ModuleB { public void testB(){ System.out.println("Method in ModuleB"); }}class ModuleC { public void testC(){ System.out.println("Method in ModuleC"); }}class Facade{ public void testA(){ ModuleA moduleA = new ModuleA(); moduleA.testA(); } public void testB(){ ModuleB moduleB = new ModuleB(); moduleB.testB(); } public void testC(){ ModuleC moduleC = new ModuleC(); moduleC.testC(); }} public class MainTest { public static void main(String arg[]) { Facade facade = new Facade(); facade.testA(); facade.testB(); facade.testC(); }} In the above code, the Facade class acts as the appearance interface of ModuleA, ModuleB, and ModuleC modules. Through this class, the client does not need to call the ABC module of the subsystem in person, nor does it need to know the details inside the system, thus better implementing the decoupling between the client and the system.
At the same time, using the appearance mode, the method can be optionally exposed. The methods defined in a module can be divided into two parts, partly for use outside the subsystem, and partly when the modules inside the subsystem are called each other.
Advantages of Appearance Mode
The appearance pattern loosens the coupling relationship between the client and the subsystem, making it easier to expand and maintain modules within the subsystem.
Make the subsystem easier to use. Clients no longer need to understand the implementation of the subsystem, nor do they need to interact with many internal modules of the subsystem. They only need to interact with appearance classes.
It can help us better divide the levels of access. Some methods are outside the system, while others are used internally. Concentrate functions that need to be exposed to the outside into the storefront, which is not only convenient for the client to use, but also hides internal details well.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.