The appearance pattern is to solve the dependency of class and class, like spring, the relationship between classes and classes can be configured into the configuration file, and the appearance pattern is to put their relationship in a facade class, reducing the coupling between class classes, which does not involve interfaces, Look at the class diagram: (We take the startup process of a computer as an example)
Let's look at the implementation class first:
[Java]View PlainCopy
- public class cpu {
-
- public void startup () {
- system.out.println ( "cpu startup!");
-     }  
-
- public void shutdown () {
- system.out.println ( "Cpu shutdown !");
-     }  
- }
[Java]View PlainCopy
- public class memory {
-
- public void startup () {
- system.out.println ( "memory startup!");
-     }  
-
- public void shutdown () {
- system.out.println ( "memory Shutdown! ");
-     }  
- }
[Java]View PlainCopy
- public class disk {
-
- public void startup () {
- system.out.println ( "disk startup!");
-     }  
-
- public void shutdown () {
- system.out.println ( "disk Shutdown! ");
-     }  
- }
[Java]View PlainCopy
- Public class Computer {
- private CPU CPU;
- private memory memory;
- private disk disk;
- Public computer () {
- CPU = new CPU ();
- Memory = new Memory ();
- Disk = new disk ();
- }
- public void Startup () {
- System.out.println ("Start the computer!");
- Cpu.startup ();
- Memory.startup ();
- Disk.startup ();
- System.out.println ("Start computer finished!");
- }
- public void shutdown () {
- SYSTEM.OUT.PRINTLN ("begin to close the computer!");
- Cpu.shutdown ();
- Memory.shutdown ();
- Disk.shutdown ();
- SYSTEM.OUT.PRINTLN ("Computer closed!");
- }
- }
The user class is as follows:
[Java]View PlainCopy
- Public class User {
- public static void Main (string[] args) {
- Computer computer = new computer ();
- Computer.startup ();
- Computer.shutdown ();
- }
- }
Output:
Start the computer!
CPU startup!
Memory startup!
Disk startup!
Start Computer finished!
Begin to close the computer!
CPU shutdown!
Memory shutdown!
Disk shutdown!
Computer closed!
If we do not have computer class, then, CPU, Memory, disk they will hold each other instance, the relationship, which will cause serious dependence, modify a class, may bring other classes of modification, this is not what we want to see, with the Computer class, The relationship between them is placed in the computer class, so that the understanding of the role of decoupling, which is the appearance of the pattern!
Java design mode-appearance mode (facade)