The appearance pattern is to solve the dependency between classes and classes, like spring, the relationship between classes and classes can be configured into a 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)
public class CPU {public void startup () {System.out.println ("CPU startup!");} public void shutdown () {System.out.println ("CPU shutdown!");}}
public class Disk {public void startup () {System.out.println ("Disk startup!");} public void shutdown () {System.out.println ("Disk shutdown!");}}
public class memory {public void startup () {System.out.println ("memory startup!");} Public void shutdown () {System.out.println ("memory shutdown!");}}
public class computer {private cpu cpu;private memory memory;private Disk disk; public computer () { this.cpu=new cpu (); this.disk=new disk (); this.memory=new memory (); } public void startup () { system.out.println ("computer startup!"); cpu.startup (); memory.startup (); Disk.startup (); system.out.println ("start computer finish!"); } public void shutdown () { system.out.println ("begin to close the Computer! "); cpu.shutdown (); memory.shutdown (); disk.shutdown (); system.out.println ("computer close!"); }}
public class User {public static void main (string[] args) {computer computer =new computer (); Computer.startup (); computer. Shutdown ();}}
Test results:
Computer startup!
CPU startup!
Memory startup!
Disk startup!
Start Computer finish!
Begin to close the computer!
CPU shutdown!
Memory shutdown!
Disk shutdown!
Computer close!
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!
Appearance mode (facade)