门面模式要求一个子系统的外部与其内部的通信必须通过一个统一的门面对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。
用一个例子进行使用,如果把中国大陆的医院作为一个子系统,按照部门职能,这个系统可以划分为挂号、门诊、划价、化验、收费、取药等。看病的病人要与这些部门打交道,就如同一个子系统的客户端与一个子系统的各个类打交道一样,不是一件容易的事情。解决这种不便的方法便是引进门面模式,可以设置一个接待员的位置,由接待员负责代为挂号、划价等。这个接待员就是门面模式的体现,病人只接触接待员,由接待员负责与医院的各个部门打交道。
在门面模式中,主要有三类角色:
子系统:子系统中包括各种各样的部门,一般认为,它们之间的逻辑关系复杂,不适合客户直接打交道;
门面:门面是子系统的专家,熟悉子系统中各部门的关系,知道如何与它们打交道,它提供易用的功能给客户,方便用户使用。
客户:系统的使用者,不想关心子系统的细节。
下面是一个门面模式的示例,其中各部门Department*都是单例类,因为部门都是唯一的一个机构;门面类Facade不是单例的,同时提供多个门面类比较合理;门面类的作用只是为用户提供方便,并不是对子系统的封装,用户可以选择不用门面类,而直接与子系统打交道,所以子系统中的各个类是可以单独被使用的。
package
facade;
public interface Server ... {
public void serv();
}
package facade;
public class DepartmentA implements Server ... {
private static DepartmentA me=null;
private DepartmentA () ...{
}
public static DepartmentA getIns() ...{
if (me==null)
me = new DepartmentA();
return me;
}
public void serv() ...{
System.out.println(this + ".serv()");
}
}
package facade;
public class DepartmentB implements Server ... {
private static DepartmentB me=null;
private DepartmentB () ...{
}
public static DepartmentB getIns() ...{
if (me==null)
me = new DepartmentB();
return me;
}
public void serv() ...{
System.out.println(this + ".serv()");
}
}
package facade;
public class DepartmentC implements Server ... {
private static DepartmentC me=null;
private DepartmentC () ...{
}
public static DepartmentC getIns() ...{
if (me==null)
me = new DepartmentC();
return me;
}
public void serv() ...{
System.out.println(this + ".serv()");
}
}
package facade;
public class Facade ... {
private DepartmentA a = DepartmentA.getIns();
private DepartmentB b = DepartmentB.getIns();
private DepartmentC c = DepartmentC.getIns();
public void guide() ...{
a.serv();
b.serv();
c.serv();
}
}
package facade;
public class Client ... {
public static void main(String[] args) ...{
(new Facade()).guide();
}
}
public interface Server ... {
public void serv();
}
package facade;
public class DepartmentA implements Server ... {
private static DepartmentA me=null;
private DepartmentA () ...{
}
public static DepartmentA getIns() ...{
if (me==null)
me = new DepartmentA();
return me;
}
public void serv() ...{
System.out.println(this + ".serv()");
}
}
package facade;
public class DepartmentB implements Server ... {
private static DepartmentB me=null;
private DepartmentB () ...{
}
public static DepartmentB getIns() ...{
if (me==null)
me = new DepartmentB();
return me;
}
public void serv() ...{
System.out.println(this + ".serv()");
}
}
package facade;
public class DepartmentC implements Server ... {
private static DepartmentC me=null;
private DepartmentC () ...{
}
public static DepartmentC getIns() ...{
if (me==null)
me = new DepartmentC();
return me;
}
public void serv() ...{
System.out.println(this + ".serv()");
}
}
package facade;
public class Facade ... {
private DepartmentA a = DepartmentA.getIns();
private DepartmentB b = DepartmentB.getIns();
private DepartmentC c = DepartmentC.getIns();
public void guide() ...{
a.serv();
b.serv();
c.serv();
}
}
package facade;
public class Client ... {
public static void main(String[] args) ...{
(new Facade()).guide();
}
}