http://www.bianceng.cn/Programming/project/201111/31621_2.htm
http://blog.sina.com.cn/s/blog_5f1b067a0100ecak.html
门面模式有三个角色组成:
1)<wbr><wbr><wbr><wbr><wbr><wbr><wbr>门面角色( facade):这是门面模式的核心。它被客户角色调用,因此它熟悉子系统的功能。它内部根据客户角色已有的需求预定了几种功能组合。</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
2)<wbr><wbr><wbr><wbr><wbr><wbr><wbr>子系统角色:实现了子系统的功能。对它而言, façade 角色就和客户角色一样是未知的,它没有任何 façade角色的信息和链接。</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
3)<wbr><wbr><wbr><wbr><wbr><wbr><wbr>客户角色:调用 façade 角色来完成要得到的功能</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
模式作用:
将一些复杂的操作封装起来,以一个简单的接口提供给客户端.
可以定义多个子系统, 层次之间的粗细粒度需要把握好.一个子系统一个门面类
模式应用:
<wbr><wbr><wbr><wbr><wbr><wbr>Facade 一个典型应用就是进行数据库连接。一般我们在每一次对数据库进行访问,都要进行以下操作:先得到 connect实例,然后打开 connect 获得连接,得到一个 statement ,执行 sql 语句进行查询,得到查询结果集。</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>我们可以将这些步骤提取出来,封装在一个类里面。这样,每次执行数据库访问只需要将必要的参数传递到这个类中就可以了。</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
代码:
public class ShopingCenter
{
//到购物中心的服饰店购买
public void clothingStore()
{
Clothing c = new Clothing();
c.getClothing(); //购买到衣服
}
//到购物中心的电器店购买
public void EleStore()
{
Ele c = new Ele();
c.getEle(); //购买到电器
}
//到购物中心的首饰店购买
public void JewelryStore()
{
Jewelry c = new Jewelry();
c.getJewelry(); //购买到首饰
}
}
客户端代码:
ShopingCenter sc = new ShopingCenter();
sc.clothingStore();
sc.EleStore();
sc.JewelryStore();
看这个例子:
package pattern.facade;
/**
* 门面模式/外观模式:Facade Pattern
*
* 保安系统:
* 一个保安系统由两个录像机、一个感应器和一个报警器组成。
* 由保安操作仪器的启动和关闭:没有使用门面模式时,保安必须亲自启动每个仪器。
* @version 2009-6-15
* @author Winty(wintys@gmail.com)
*/
public class FacadeTest{
public static void main(String[] args){
//无门面模式
Camera camera1,camera2;
camera1 = new Camera();
camera2 = new Camera();
Sensor sensor;
sensor = new Sensor();
Alarm alarm;
alarm = new Alarm();
//启动仪器
camera1.turnOn();
camera2.turnOn();
sensor.activate();
alarm.activate();
System.out.println("");
/
//使用门面模式
SecurityFacade security = new SecurityFacade();
security.start();
}
}
/**
* 门面:Facade
*/
class SecurityFacade{
private Camera camera1;
private Camera camera2;
private Sensor sensor;
private Alarm alarm;
public SecurityFacade(){
camera1 = new Camera();
camera2 = new Camera();
sensor = new Sensor();
alarm = new Alarm();
}
//启动
public void start(){
camera1.turnOn();
camera2.turnOn();
sensor.activate();
alarm.activate();
}
//停止
public void stop(){
camera1.turnOff();
camera2.turnOff();
sensor.deactivate();
alarm.deactivate();
}
}
class Camera{
public void turnOn(){
System.out.println("turn on the Camera.");
}
public void turnOff(){
System.out.println("turn off the Camera.");
}
//转动
public void rotate(){
System.out.println("rotate the Camera.");
}
}
class Sensor{
public void activate(){
System.out.println("activate the sensor.");
}
public void deactivate(){
System.out.println("deactivate the sensor.");
}
//触发感应器
public void trigger(){
System.out.println("trigger the sensor.");
}
}
class Alarm{
public void activate(){
System.out.println("activate the alarm.");
}
public void deactivate(){
System.out.println("deactivate the alarm.");
}
//拉响报警器
public void ring(){
System.out.println("ring the alarm.");
}
}
运行结果:
turn on the Camera.
turn on the Camera.
activate the sensor.
activate the alarm.
turn on the Camera.
turn on the Camera.
activate the sensor.
activate the alarm.