外观模式
外观模式被用来把一组复杂的类包装在一个比较简单的封装接口中。
摘自:http://www.cnblogs.com/saville/archive/2012/10/18/2730089.html
以汽车为例。首先定义引擎、车轮、刹车等子系统。
class Engine { public void Start() { Console.WriteLine("Engine Start"); } } class Wheel { public void TurnOn() { Console.WriteLine("Wheel is turned"); } public void Stop() { Console.WriteLine("Wheel is stopped"); } } class Braker { public void Brake() { Console.WriteLine("Brake!"); } }
接着定义汽车的Facade对象。
class CarFacade { private Engine _engine; private Wheel[] _wheels; private Braker _braker; public CarFacade() { _engine = new Engine(); _wheels = new Wheel[4]; for (int i = 0; i < _wheels.Length; i++) { _wheels[i] = new Wheel(); } _braker = new Braker(); } public void Run() { _engine.Start(); foreach (Wheel wheel in _wheels) { wheel.TurnOn(); } } public void Brake() { _braker.Brake(); foreach (Wheel wheel in _wheels) { wheel.Stop(); } } }
只需要使用Façade对象即可实现对汽车的操作。
static void Main(string[] args) { CarFacade car = new CarFacade(); car.Run(); car.Brake(); Console.ReadLine(); }