外部與一個子系統的通訊必須經過一個統一的門面(Facade)對象進行,這就是門面模式。外觀模式爲子系統提供了統一的界面, 屏蔽了子類的不一樣javascript
現代大型軟件發展到必定程度會很是複雜, 因而就須要對軟件進行模塊化開發, 將系統分紅各個模塊, 有利於維護和拓展,但即便這樣在咱們調用的時候依然要和許多類打交道, 依然很複雜, 因而外觀設計模式應運而生. 外觀設計模式就是爲多個子系統提供一個統一的外觀類來簡化外部人員操做, 下面是使用外觀模式先後的的醫院案例java
外觀模式就好像一個接待員免去了外部人員與各個科室進行交流, 應爲各個科室運做流程仍是比較複雜的, 經過一個熟悉業務的外觀類能夠大大提升醫院的效率.設計模式
外觀模式設計兩種對象模塊化
外觀模式調用方式
this
未使用外觀模式spa
子系統類prototype
let Light = function () { }; Light.prototype.turnOn = function () { console.log('Light turn on'); }; Light.prototype.turnOff = function () { console.log('Light turn off'); }; let TV = function () { }; TV.prototype.turnOn = function () { console.log('TV turn on'); }; TV.prototype.turnOff = function () { console.log('TV turn off'); }; let Computer = function () { }; Computer.prototype.turnOn = function () { console.log('Computer turn on'); }; Computer.prototype.turnOff = function () { console.log('Computer turn off'); };
客戶端調用設計
let light = new Light(); let tv = new TV(); let computer = new Computer(); light.turnOn(); tv.turnOn(); computer.turnOn(); light.turnOff(); tv.turnOff(); computer.turnOff();
使用外觀模式code
子系統類模塊化開發
//同上
外觀類
let Facade = function () { this.light = new Light(); this.tv = new TV(); this.computer = new Computer(); } Facade.prototype.turnOn = function () { light.turnOn(); tv.turnOn(); computer.turnOn(); } Facade.prototype.turnOff = function () { light.turnOff(); tv.turnOff(); computer.turnOff(); }
客戶端調用
let facade = new Facade(); facade.turnOn(); facade.turnOff();