外观模式(Facade Pattern):外部与一个子系统的通讯必须经过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。外观模式又称为门面模式,它是一种对象结构型模式。git
Facade类:github
namespace FacadePattern.BasicStructure { /// <summary> /// 外观类 /// </summary> class Facade { private SubSystemOne _subSystemOne; private SubSystemTwo _subSystemTwo; private SubSystemThree _subSystemThree; private SubSystemFour _subSystemFour; public Facade() { _subSystemOne = new SubSystemOne(); _subSystemTwo = new SubSystemTwo(); _subSystemThree = new SubSystemThree(); _subSystemFour = new SubSystemFour(); } public void MethodA() { Console.WriteLine("\n方法组A() ---"); _subSystemOne.Method(); _subSystemTwo.Method(); _subSystemFour.Method(); } public void MethodB() { Console.WriteLine("\n方法组B() ---"); _subSystemTwo.Method(); _subSystemThree.Method(); } } }
SubSystemOne类:spa
namespace FacadePattern.BasicStructure { /// <summary> /// 子系统一 /// </summary> class SubSystemOne { public void Method() { Console.WriteLine("子系统一的方法"); } } }
SubSystemTwo类:代理
namespace FacadePattern.BasicStructure { /// <summary> /// 子系统二 /// </summary> class SubSystemTwo { public void Method() { Console.WriteLine("子系统二的方法"); } } }
SubSystemThree类:code
namespace FacadePattern.BasicStructure { /// <summary> /// 子系统三 /// </summary> class SubSystemThree { public void Method() { Console.WriteLine("子系统三的方法"); } } }
SubSystemFour类:对象
namespace FacadePattern.BasicStructure { /// <summary> /// 子系统四 /// </summary> class SubSystemFour { public void Method() { Console.WriteLine("子系统四的方法"); } } }
客户端调用代码:blog
static void Main(string[] args) { try { {//BasicStructure //因为Facade的做用,客户端根本不知道子4个子系统的存在 Facade facade = new Facade(); facade.MethodA(); facade.MethodB(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
结果以下:接口
场景模拟:本人天天都要给笔记本电脑开机关机,接下来就用外观模式来模拟下。get
NotebookPowerSupply(笔记本电源)类——Facade类string
namespace FacadePattern.SituationSimulation { /// <summary> /// 笔记本电脑电源键--至关于外观类 /// </summary> class NotebookPowerSupply { private OpenPowerSupply _openPowerSupply; private ClosePowerSupply _closePowerSupply; public NotebookPowerSupply() { _openPowerSupply = new OpenPowerSupply(); _closePowerSupply = new ClosePowerSupply(); } public void Open() { Console.WriteLine("\n--开启电源方法"); _openPowerSupply.Operation(); } public void Close() { Console.WriteLine("\n--关闭电源方法"); _closePowerSupply.Operation(); } } }
OpenPowerSupply(开启电源)类——SubSystem类
namespace FacadePattern.SituationSimulation { /// <summary> /// 打开电源 /// </summary> class OpenPowerSupply { public void Operation() { Console.WriteLine("打开电源"); } } }
ClosePowerSupply(关闭电源)类——SubSystem类
namespace FacadePattern.SituationSimulation { /// <summary> /// 关闭电源 /// </summary> class ClosePowerSupply { public void Operation() { Console.WriteLine("关闭电源"); } } }
客户端调用代码:
static void Main(string[] args) { try { {//SituationSimulation NotebookPowerSupply notebookPowerSupply = new NotebookPowerSupply(); notebookPowerSupply.Open(); notebookPowerSupply.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
结果以下:
外观模式的优势
外观模式的缺点
在如下状况下可使用外观模式: