外观模式--Java实现
画类图
在计算机中,只要按下计算机的启动按钮,就能自动调用计算机内部的内存自检(Memory check())、CPU的运行(run())、硬盘的读取(HardDisk read())、操作系统的载入(OS load())等,如果某一个过程发生错误,那么整个系统都会启动失败!
具体代码实现
//MainFrame.java
package org.example.design009;
import org.example.design001.Main;
public class MainFrame {
public MainFrame() {
memory = new Memory();
cpu = new CPU();
hardDisk = new HardDisk();
os = new OS();
}
private Memory memory;
private CPU cpu;
private HardDisk hardDisk;
private OS os;
public void start(){
if(memory.check()&&cpu.run()&&hardDisk.read()&&os.load()){
System.out.println("计算机启动成功!");
}else{
System.out.println("计算机启动失败!");
}
}
}
//Memory.java
package org.example.design009;
public class Memory {
public boolean check(){
System.out.println("内存读取成功!");
return true;
}
}
//CPU.java
package org.example.design009;
public class CPU {
public boolean run(){
System.out.println("CPU运行成功!");
return true;
}
}
//HardDisk.java
package org.example.design009;
public class HardDisk {
public boolean read(){
System.out.println("硬盘读取成功!");
return true;
}
}
//OS.java
package org.example.design009;
public class OS {
public boolean load(){
System.out.println("操作系统载入成功!");
return true;
}
}
//Client.java
package org.example.design009;
public class Client {
public static void main(String[] args) {
MainFrame mainFrame=new MainFrame();
mainFrame.start();
}
}