外观模式又被称为Facade模式,解决类之间的耦合,将所有的类的关系放置在一个Facade类中
public class Facade {
public static void main(String[]args){
Computer computer = new Computer();
computer.startUp();
computer.shutDown();
}
}
class Cpu {
public void startUp() {
System.out.println("cpu startup");
}
public void shutDown() {
System.out.println("cpu shutdown");
}
}
class Memory {
public void startUp() {
System.out.println("memory startup");
}
public void shutDown() {
System.out.println("memory shutdown");
}
}
class Disk {
public void startUp() {
System.out.println("disk startup");
}
public void shutDown() {
System.out.println("disk shutdown");
}
}
class Computer {
private Cpu cpu;
private Memory memory;
private Disk disk;
public Computer() {
this.cpu = new Cpu();
this.memory = new Memory();
this.disk = new Disk();
}
public void startUp() {
System.out.println("this computer start up");
cpu.startUp();
memory.startUp();
disk.startUp();
System.out.println("start computer success");
}
public void shutDown() {
System.out.println("begin to close the comupter!");
cpu.shutDown();
memory.shutDown();
disk.shutDown();
System.out.println("cloes the computer successs");
}
}
输出的结果为: