为什么要使用外观模式
屏蔽模块内部复杂的逻辑,为调用者提供简单清晰的接口,符合六大设计原则的最小知识原则(迪米特法则)。
举个栗子
我们先看调用时候的代码,ComputerFacade屏蔽了复制的电脑开机流程,客户端只要知道电脑开机和关机的接口,具体实现他不需要关心,比如CPU上电,硬盘加载等等...
` ComputerFacade computerFacade = new ComputerFacade(); computerFacade.start(); computerFacade.shutdown();public class ComputerFacade { public CPU cpu = new CPU(); public Disk disk = new Disk(); public Memory memory = new Memory(); public void start(){ cpu.start(); disk.start(); memory.start(); } public void shutdown(){ cpu.shutdown(); disk.shutdown(); memory.shutdown(); }}public class CPU { public void start(){ System.out.println("cpu start...."); } public void shutdown(){ System.out.println("cpu shutdown...."); }}public class Disk { public void start(){ System.out.println("Disk start...."); } public void shutdown(){ System.out.println("Disk shutdown...."); }}`