应用场景:
1丶 为一个复杂子系统提供一个简单接口 只需要专注于接口即可。 例如在项目中会用到以往的代码, 可这些代码不清晰 难懂, 可以分出一人利用外观模式将老的代码封装,提供功能接口以供项目的开发,而不必每个人都去研究难懂,不清晰的代码。
2丶 提高子系统的独立性 , 降低子类和客户端的耦合
例子1
public class CPU {
public void startUp() {
System.out.println("启动CPU");
}
public void shutDown() {
System.out.println("关闭CPU");
}
}
class Memory {
public void startUp() {
System.out.println("加载内存");
}
public void shutDown() {
System.out.println("清空内存");
}
}
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() {
cpu = new CPU();
memory = new Memory();
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("关闭完成");
}
}
//-------------------
public class User {
public static void main(String[] args) {
Computer computer = new Computer();
computer.startUp();
computer.shutDown();
}
}
上述例子
cpu Memory(内存) Disk(硬盘) 即为子系统 对于操作电脑的人不必去关心这些子系统是如何工作 , Cumputer对子系统进行了封装 , 人们只需要简单的去操作Cumputer这个接口即可
输出结果:
例子2 抽屉问题
看这样的场景, 一个文件放在了第二个抽屉里,而第二个抽屉的钥匙放在了第一个抽屉里 我们要将文件取出 如果只有两个抽屉还好 钥匙n个抽屉的问题...... 对于用户来说,这些取钥匙的过程不需要知道 只需要一个按钮。。。
class DrawerOne {
public void open() {
System.out.println("打开第一个抽屉");
getKey();
}
public void getKey() {
System.out.println("得到第二个抽屉的钥匙");
}
}
class DrawerTwo {
public void open() {
System.out.println("打开了第二个抽屉");
getFile();
}
public void getFile() {
System.out.println("得到这个重要文件");
}
}
class DrawerFacade {
DrawerOne drawerOne = new DrawerOne();
DrawerTwo drawerTwo = new DrawerTwo();
public void open() {
drawerOne.open();
drawerTwo.open();
}
}
public class DrawerClient {
public static void main(String[] args) {
DrawerFacade drawer = new DrawerFacade();
drawer.open();
}
}
输出结果:
例子3 泡茶问题
//--------------子类-水
public class Water {
boolean waterIsBoiling;
public Water() {
setWaterIsBoiling(false);
System.out.println("可饮用的水准备好了");
}
public boolean isWaterIsBoiling() {
return waterIsBoiling;
}
public void setWaterIsBoiling(boolean waterIsBoiling) {
this.waterIsBoiling = waterIsBoiling;
}
public void biolFacadeWater() {
setWaterIsBoiling(true);
System.out.println("水在沸腾");
}
}
//------------------------子类-茶叶包
public class TeaBag {
public TeaBag() {
System.out.println("茶包准备好了");
}
}
//--------------子类-茶杯
public class TeaCup {
private boolean teaBagIsSteeped;
private Water facadeWater;
private TeaBag facadeTeaBag;
public TeaCup() {
setTeaBagIsSteeped(true);
System.out.println("茶杯准备好了");
}
public boolean getTeaBagIsSteeped() {
return teaBagIsSteeped;
}
public void setTeaBagIsSteeped(boolean teaBagIsSteeped) {
this.teaBagIsSteeped = teaBagIsSteeped;
}
public void addFacadeWaterBag(TeaBag facadeTeaBagIn) {
facadeTeaBag = facadeTeaBagIn;
System.out.println("茶包放进茶杯了");
}
public void addFacadeWater(Water facadeWaterIn) {
facadeWater = facadeWaterIn;
System.out.println("水准备好了");
}
public void steepTeaBag() {
if (facadeWater != null && facadeTeaBag != null) {
System.out.println("茶叶被热水泡了");
setTeaBagIsSteeped(true);
}
else {
System.out.println("茶叶没有被热水泡");
setTeaBagIsSteeped(false);
}
}
public String toString() {
if (getTeaBagIsSteeped()) {
return "一杯又浓又香的茶水泡好了";
}
else
return "wait...";
}
}
//---外观模式的对外接口
public class FacadeCuppaMaker {
private boolean TeaBagIsSteeped;
public FacadeCuppaMaker() {
System.out.println("准备冲茶了");
}
public TeaCup makeACuppa() {
TeaCup cup = new TeaCup();
TeaBag teaBag = new TeaBag();
Water water = new Water();
cup.addFacadeWater(water);
water.biolFacadeWater();
cup.addFacadeWaterBag(teaBag);
cup.steepTeaBag();
return cup;
}
}
//-----------------------------执行 外观接口
public class Client {
public static void main(String[] args) {
FacadeCuppaMaker cuppaMaker = new FacadeCuppaMaker();
TeaCup teaCup = cuppaMaker.makeACuppa();
System.out.println(teaCup);
}
}
输出结果: