软件设计-Tutorial12
package Tutorial12; // 定义各个硬件设备和软件的类 class Memory { public boolean check() { System.out.println("Memory self-checking..."); // 假设返回true表示自检成功 return true; } } class CPU { public boolean run() { System.out.println("CPU running..."); // 假设返回true表示CPU运行成功 return true; } } class HardDisk { public boolean read() { System.out.println("Hard Disk reading..."); // 假设返回true表示硬盘读取成功 return true; } } class OS { public boolean load() { System.out.println("Operating System loading..."); // 假设返回true表示系统载入成功 return true; } } // 创建外观类 ComputerFacade,用于封装开机的复杂逻辑 class ComputerFacade { private Memory memory; private CPU cpu; private HardDisk hardDisk; private OS os; public ComputerFacade() { this.memory = new Memory(); this.cpu = new CPU(); this.hardDisk = new HardDisk(); this.os = new OS(); } // 开机方法 public void on() { System.out.println("Computer starting..."); // 依次调用各个硬件和软件的启动方法,检查是否成功 if (memory.check() && cpu.run() && hardDisk.read() && os.load()) { System.out.println("Computer started successfully!"); } else { System.out.println("Computer failed to start."); } } } // 测试主机类 public class Mainframe { public static void main(String[] args) { ComputerFacade computer = new ComputerFacade(); // 按下开机按钮 computer.on(); } }