为复杂子系统提供了一个简单接口
违反了开放封闭原则
传统的操作是客户调用好多个子系统,外观模式的出现只需要客户掉外观类就行了
外观类其实就是客户端
举例
我想在家看电影,旧社会时代,我要自己去把DVD打开,把爆米花机打开,投影仪打开,屏幕放下来,立体声打开,灯光打开。
新时代出现了更便捷的方法,我把它称之为人工智能,我只需要躺着对人工智能发送命令,他就会替我实现一些类的操作。
代码实现
子系统
public class DVD
{
private static DVD instance = new DVD();
public static DVD getInstance()
{
return instance;
}
public void On()
{
Console.WriteLine("DVD ON");
}
public void Off()
{
Console.WriteLine("DVD OFF");
}
public void Play()
{
Console.WriteLine("DVD Is Playing");
}
public void Pause()
{
Console.WriteLine("DVD Is Pausing");
}
}
public class Popcorn
{
private static Popcorn instance = new Popcorn();
public static Popcorn getInstance()
{
return instance;
}
public void On()
{
Console.WriteLine("Popcorn ON");
}
public void Off()
{
Console.WriteLine("Popcorn OFF");
}
public void Pop()
{
Console.WriteLine("Popcorn Is Poping");
}
}
public class Projector
{
private static Projector instance = new Projector();
public static Projector getInstance()
{
return instance;
}
public void On()
{
Console.WriteLine("Projector ON");
}
public void Off()
{
Console.WriteLine("Projector OFF");
}
public void Focus()
{
Console.WriteLine("Projector Is Focusing");
}
}
public class Screen
{
private static Screen instance = new Screen();
public static Screen getInstance()
{
return instance;
}
public void Up()
{
Console.WriteLine("Screen UP");
}
public void Down()
{
Console.WriteLine("Screen Down");
}
}
public class Stereo
{
private static Stereo instance = new Stereo();
public static Stereo getInstance()
{
return instance;
}
public void On()
{
Console.WriteLine("Stereo On");
}
public void Off()
{
Console.WriteLine("Stereo Off");
}
public void Up()
{
Console.WriteLine("Stereo UP");
}
public void Down()
{
Console.WriteLine("Stereo Down");
}
}
public class TheaterLight
{
private static TheaterLight instance = new TheaterLight();
public static TheaterLight getInstance()
{
return instance;
}
public void On()
{
Console.WriteLine("TheaterLight On");
}
public void Off()
{
Console.WriteLine("TheaterLight Off");
}
public void Bright()
{
Console.WriteLine("TheaterLight Bright");
}
public void Dim()
{
Console.WriteLine("TheaterLight Dim");
}
}
人工智能(外观类)
public class HomeTheaterFacade
{
private DVD dvd;
private Popcorn popcorn;
private Projector projector;
private Screen screen;
private Stereo stereo;
private TheaterLight theaterLight;
public HomeTheaterFacade()
{
dvd = DVD.getInstance();
popcorn = Popcorn.getInstance();
projector = Projector.getInstance();
screen = Screen.getInstance();
stereo = Stereo.getInstance();
theaterLight = TheaterLight.getInstance();
}
public void Ready()
{
popcorn.On();
popcorn.Pop();
screen.Down();
projector.On();
stereo.On();
dvd.On();
theaterLight.Dim();
}
public void Play()
{
dvd.Play();
}
public void Pause()
{
dvd.Pause();
}
public void End()
{
popcorn.Off();
theaterLight.Bright();
screen.Up();
projector.Off();
stereo.Off();
dvd.Off();
}
}
调用
class Program
{
//家庭影院演示
static void Main(string[] args)
{
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.Ready();//准备
homeTheaterFacade.Play();//开始
homeTheaterFacade.Pause();//暂停
homeTheaterFacade.End();//关闭
}
}
实例展示