门面模式是对象的结构模式。
门面模式要求 一个子系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行

何时使用?
1. 为一个复杂子系统提供一个简单接口
2. 子系统的独立性
3. 层次化结构

对象图示意:
『蚂蚁学模式』之实例化门面(Facade)模式_第1张图片

1. com.cleversoft.designpatterns.facade.Alarm.java:
package  com.cleversoft.designpatterns.facade;

public   class  Alarm {
    
public   void  activate() {
        System.out.println(
" Activating the alarm. " );
    }

    
public   void  deactivate() {
        System.out.println(
" Deactivating the alarm. " );
    }

    
public   void  ring() {
        System.out.println(
" Ring the alarm. " );
    }

    
public   void  stopRing() {
        System.out.println(
" Stop the alarm. " );
    }
}

2. com.cleversoft.designpatterns.facade.Camera.java:
package  com.cleversoft.designpatterns.facade;

public   class  Camera {
    
public   void  turnOn() {
        System.out.println(
" Turning on the camera. " );
    }

    
public   void  turnOff() {
        System.out.println(
" Turning off the camera. " );
    }

    
public   void  rotate( int  degrees) {
        System.out.println(
" Rotating the camera by  "   +  degrees  +   "  degrees. " );
    }
}

3. com.cleversoft.designpatterns.facade.Light.java:
package  com.cleversoft.designpatterns.facade;

public   class  Light {
    
public   void  turnOn() {
        System.out.println(
" Turning on the light. " );
    }

    
public   void  turnOff() {
        System.out.println(
" Turning off the light. " );
    }

    
public   void  changeBulb() {
        System.out.println(
" Changing the light-bulb. " );
    }
}

4. com.cleversoft.designpatterns.facade.Sensor.java:
package  com.cleversoft.designpatterns.facade;

public   class  Sensor {
    
public   void  activate() {
        System.out.println(
" Activating on the sensor. " );
    }

    
public   void  deactivate() {
        System.out.println(
" Deactivating on the sensor. " );
    }

    
public   void  trigger() {
        System.out.println(
" The sensor has been triggered. " );
    }
}

5. com.cleversoft.designpatterns.facade.SecurityFacade.java:
package  com.cleversoft.designpatterns.facade;

public   class  SecurityFacade {
    
private  Camera camera1, camera2;

    
private  Light light1, light2, light3;

    
private  Sensor sensor;

    
private  Alarm alarm;

    
public  SecurityFacade() {
        camera1 
=   new  Camera();
        camera2 
=   new  Camera();
        light1 
=   new  Light();
        light2 
=   new  Light();
        light3 
=   new  Light();
        sensor 
=   new  Sensor();
        alarm 
=   new  Alarm();
    }

    
public   void  activate() {
        camera1.turnOn();
        camera2.turnOn();

        light1.turnOn();
        light2.turnOn();
        light3.turnOn();

        sensor.activate();
        alarm.activate();
    }

    
public   void  deactivate() {
        camera1.turnOff();
        camera2.turnOff();

        light1.turnOff();
        light2.turnOff();
        light3.turnOff();

        sensor.deactivate();
        alarm.deactivate();
    }
}

6. com.cleversoft.designpatterns.facade.Client.java:
package  com.cleversoft.designpatterns.facade;

public   class  Client {
    
private   static  SecurityFacade security  =   new  SecurityFacade();

    
public   static   void  main(String[] args) {
        security.activate();
    }
}


参考资料:
1. 《Java与模式》


欢迎大家访问我的个人网站 萌萌的IT人