标签:实例 image this print info memory ring com class
外观模式是为了解决类与类之间的依赖关系的,就是把那些类的实例都放在一个Facade类中,降低类之间的耦合度
public class CPU { public void startup(){ System.out.println("CPU 启动"); } public void shutdown(){ System.out.println("CPU 关闭"); } } public class Memory { public void startup(){ System.out.println("内存启动"); } public void shutdown(){ System.out.println("内存关闭"); } } public class Disk { public void startup(){ System.out.println("磁盘启动"); } public void shutdown(){ System.out.println("磁盘关闭"); } } public 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("电脑启动开始"); cpu.startup(); memory.startup(); disk.startup(); System.out.println("电脑启动完成"); } public void shutdown(){ System.out.println("电脑关闭开始"); cpu.shutdown(); memory.shutdown(); disk.shutdown(); System.out.println("电脑关闭完成"); } }
有了compute类的作用,cpu,memory,disk这些类之间的关系就被放在computer中,达到解耦的作用,这就是外观模式
public class Test { public static void main(String[] args) { Computer computer = new Computer(); computer.startup(); System.out.println("======================"); computer.shutdown(); } }
标签:实例 image this print info memory ring com class
原文地址:https://www.cnblogs.com/xiaobo520/p/10508341.html