门面设计模式隐藏自身的复杂功能转而提供一个简单的接口。电脑的开机过程就是一个很好的例子。电脑在开机时会调用CPU、内存、硬盘。为了方便用户,我们可以添加一个门面(facade),封装这些复杂的任务调度,并且提供一个简单的接口。
译者想到了一个更好的例子:大家在去医院看病的时候,会经历挂号、门诊、划价、收费、化验、取药等流程,往往这些职能部门在医院都是独立、分散设置的,病人一一地与这些独立部门打交道,显然不是一件容易的事情。比如,首先病人要挂号,然后门诊。如果医生要求化验,病人首先必须划价,然后缴费,才能到化验室做化验。化验完后,再回到门诊室。
解决这种办法就可以引进门面模式。对于医院来说,我们可以在大门前设置一个接待员(现在有效率的医院往往都是这样),由接待员负责代为挂号、划价、收费、取药等,病人只接触接待员,由接待员与其他部门去沟通。
1、类图
2、源码
//the components of a computer
class CPU {
public void processData() { }
}
class Memory {
public void load() { }
}
class HardDrive {
public void readdata() { }
}
/* Facade */
class Computer {
private CPU cpu;
private Memory memory;
private HardDrive hardDrive;
public Computer() {
this.cpu = new CPU();
this.memory = new Memory();
this.hardDrive = new HardDrive();
}
public void run() {
cpu.processData();
memory.load();
hardDrive.readdata();
}
}
class User {
public static void main(String[] args) {
Computer computer = new Computer();
computer.run();
}
}
3、项目中的应用
In javax.faces.context, ExternalContext internally uses ServletContext, HttpSession, HttpServletRequest, HttpServletResponse, etc. It allows the Faces API to be unaware of the nature of its containing application environment.