一个保安系统的,由录像机、电灯、红外线监控和警报器组成。保安系统的操作人员需要经常将这些仪器启动和关闭。保安类需要用到所有的录像机(Camera)、电灯(Light)、感应器(Sensor)和警报器(Alarm)对象,保安觉得使用不方便。应用外观模式,用C#控制台应用程序改进该设计。绘制该模式的UML图。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shiyan8waiguanmoshi
{
class Program
{
public class Camera
{
public void TurnOn()
{
Console.WriteLine(“Turning on the camera.”);
}
public void TurnOff()
{
Console.WriteLine(“Turning off the camera.”);
}
}
public class Light
{
public void TurnOff()
{
Console.WriteLine(“Turning on the light.”);
}
public void TurnOn()
{
Console.WriteLine(“Turning off the light.”);
}
}
public class Sensor
{
public void Activate()
{
Console.WriteLine(“Activating the sensor.”);
}
public void Deactivate()
{
Console.WriteLine(“Deactivating the sensor.”);
}
public void Trigger()
{
Console.WriteLine(“The sensor has triggered.”);
}
}
public class Alarm
{
public void Activate()
{
Console.WriteLine(“Activating the alarm.”);
}
public void Deactivate()
{
Console.WriteLine(“Deactivating the alarm.”);
}
public void Ring()
{
Console.WriteLine(“Ringing the alarm.”);
}
public void StopRing()
{
Console.WriteLine(“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();
}
}
private static SecurityFacade security = new SecurityFacade();
static void Main(String[] args)
{
security.Activate();
Console.WriteLine(“\n--------------------\n”);
security.Deactivate();
}
}
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』