您的位置:首页 > 其它

Facade(门面、外观)模式

2017-03-30 19:41 323 查看
Facade pattern 这个名字是类比建筑学中的 facade 而来的。 

一个 facade 就是一个对象,这个对象对一大块代码(例如一个类库)进行了封装,提供了一个简化的接口。facade 的优点:
由于 facade 为常用的任务提供了更方便的方法,所以让类库更容易使用、理解和测试;
同样地,由于 facade 为常用的任务提供了更方便的方法,所以让类库的可读性更好。
由于外部代码中的大部分代码是与 facade 在打交道而不是直接与类库打交道,所以减少了外部代码对类库内部的工作方式的依赖,同时使系统的开发更灵活。
把一个设计糟糕的 APIs 集合包裹在单个设计良好的接口中。

当系统非常复杂或者是由于系统有大量相互依赖的类或者是其他原因导致系统很难理解时就可以使用 Facade pattern 。这种模式隐藏了大型系统的复杂性,为客户端提供了一个相对简单的接口。Facade pattern 典型的涉及到了一个单一的包装器类,这个包装器中会有一组变量,客户端需要的功能会由这组变量中的一个或多个协同来完成,这些成员变量代理 facade 去访问系统,这样一来对客户端就隐藏了实现细节。

例子:

using UnityEngine;
using System.Collections;

/*
*我们考察一个保安系统的例子,以说明门面模式的功效。一个保安系统由两个录像机、
*三个电灯、一个遥感器和一个警报器组成。保安系统的操作人员需要经常将这些仪器启动和关闭。
*在不使用门面模式的情况下,操作这个保安系统的操作员必须直接操作所有的这些部件
*/
public class Camera
{
public void TurnOn()
{
Debug.Log("Turning on the Camera.");
}

public void TurnOff()
{
Debug.Log("Turning off the Camera.");
}

public void Rotate(int degrees)
{
Debug.Log("Rotating the Camera by"+degrees+"degrees.");
}
}

public class Light
{

public void TurnOn()
{
Debug.Log("Turning on the Light.");
}

public void TurnOff()
{
Debug.Log("Turning off the Light.");
}

public void ChangeBulb()
{
Debug.Log("changing the Light-bulb.");
}
}

public class Sensor
{
public void Activate()
{
Debug.Log("Activating the Sensor.");
}

public void Deactivate()
{
Debug.Log("Deactivating the Sensor.");
}

public void Trigger()
{
Debug.Log("The Sensor has triggered.");
}
}

public class Alarm
{

public void Activate()
{
Debug.Log("Activating the alarm.");
}

public void Deactivate()
{
Debug.Log("Deactivating the alarm.");
}

public void Ring()
{
Debug.Log("Ringing the alarm.");
}

public void StopRing()
{
Debug.Log("Stop the alarm.");
}
}

public class TestFacade : MonoBehaviour {
private static Camera camera1, camera2;
private static Light light1, light2, light3;
private static Sensor Sensor;
private static Alarm alarm;

void Start () {
/*保安直接操控*/
camera1 = new Camera();
camera2 = new Camera();
light1 = new Light();
light2 = new Light();
light3 = new Light();
Sensor = new Sensor();
alarm = new Alarm();
camera1.TurnOn();
camera2.TurnOn();
light1.TurnOn();
light2.TurnOn();
light3.TurnOn();
Sensor.Activate();
alarm.Activate();
}

void Update () {

}
}

在使用门面模式的情况下,我们准备一个保安系统的控制台,作为保安操控的界面。

using UnityEngine;
using System.Collections;

public class Camera
{
public void TurnOn()
{
Debug.Log("Turning on the camera.");
}

public void TurnOff()
{
Debug.Log("Turning off the camera.");
}

public void Rotate(int degrees)
{
Debug.Log("Rotating the camera by"+degrees+"degrees.");
}
}

public class Light
{

public void TurnOff()
{
Debug.Log("Turning off the light.");
}

public void TurnOn()
{
Debug.Log("Turning on the light.");
}

public void ChangeBulb()
{
Debug.Log("changing the light-bulb.");
}
}

public class Sensor
{
public void Activate()
{
Debug.Log("Activating the sensor.");
}

public void Deactivate()
{
Debug.Log("Deactivating the sensor.");
}

public void Trigger()
{
Debug.Log("The sensor has triggered.");
}
}

public class Alarm
{

public void Activate()
{
Debug.Log("Activating the alarm.");
}

public void Deactivate()
{
Debug.Log("Deactivating the alarm.");
}

public void Ring()
{
Debug.Log("Ringing the alarm.");
}

public void StopRing()
{
Debug.Log("Stop the alarm.");
}
}

public class SecurityFacade
{
private static Camera camera1, camera2;
private static Light light1, light2, light3;
private static Sensor sensor;
private static Alarm alarm;

static 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();
}
}

public class TestFacade : MonoBehaviour {
private static SecurityFacade security;
void Start () {
/*保安利用控制台操控*/
security = new SecurityFacade();
security.Activate();
security.Deactivate();
}

void Update () {

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: