[关键字]:java,design pattern,设计模式,《Java与模式》学习,门面模式,外观模式,Facade Pattern
[环境]:StarUML5.0 + JDK6
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys/
[正文]:
无门面模式时:
![](http://dl.iteye.com/upload/attachment/153605/361e76b6-bdf3-3c6c-9788-422a687843ee.jpg)
有门面模式时:
![](http://dl.iteye.com/upload/attachment/153603/b46f684b-c0e2-32ee-8e3c-09a5a4687622.jpg)
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.");
}
}
/**
* 门面模式/外观模式: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.