複製鏈接
請複製以下鏈接發送給好友

外觀模式

鎖定
外觀模式(Facade),亦稱“過程模式”。學校課程評價模式之一。美國教育學者斯泰克1967 年在所著《教育評價的外觀》中提出。主張按照描述和判斷資料來評價課程,關鍵的活動是在課程實施的全過程中進行觀察和蒐集意見,以瞭解人們對課程的不同看法。這種模式不限於檢查教學的成果,重視描述和判斷教學過程中各種複雜、動態的現象和事物。
中文名
外觀模式
外文名
Facade
類    型
組合型
學    科
計算機

目錄

外觀模式結構

這個外觀類為子系統提供一個共同的對外接口
Clients
客户對象通過一個外觀接口讀寫子系統中各接口的數據資源。 [1] 

外觀模式適用場景

在以下情況下可以考慮使用外觀模式:
(1)設計初期階段,應該有意識的將不同層分離,層與層之間建立外觀模式。
(2) 開發階段,子系統越來越複雜,增加外觀模式提供一個簡單的調用接口。
(3) 維護一個大型遺留系統的時候,可能這個系統已經非常難以維護和擴展,但又包含非常重要的功能,為其開發一個外觀類,以便新系統與其交互。 [1] 

外觀模式優點

(1)實現了子系統與客户端之間的松耦合關係。
(2)客户端屏蔽了子系統組件,減少了客户端所需處理的對象數目,並使得子系統使用起來更加容易。 [2] 

外觀模式示例

外觀模式Java

這是一個抽象的示例。一個客户“you”通過外觀接口“computer”獲取計算機內部複雜的系統信息
/* Complex parts */class CPU {    public void freeze() { ... }    public void jump(long position) { ... }    public void execute() { ... }}class Memory {    public void load(long position, byte[] data) {        ...    }}class HardDrive {    public byte[] read(long lba, int size) {        ...    }}/* Façade */class Computer {    public void startComputer() {        cpu.freeze();        memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));        cpu.jump(BOOT_ADDRESS);        cpu.execute();    }}/* Client */class You {    public static void main(String[] args) {        Computer facade = new Computer();        facade.startComputer();    }}

外觀模式C#

// Facade pattern -- Structural example using System;namespace DoFactory.GangOfFour.Facade.Structural{  // Mainapp test application   class MainApp  {    public static void Main()    {      Facade facade = new Facade();      facade.MethodA();      facade.MethodB();      // Wait for user       Console.Read();    }  }  // "Subsystem ClassA"   class SubSystemOne  {    public void MethodOne()    {      Console.WriteLine(" SubSystemOne Method");    }  }  // Subsystem ClassB"   class SubSystemTwo  {    public void MethodTwo()    {      Console.WriteLine(" SubSystemTwo Method");    }  }  // Subsystem ClassC"   class SubSystemThree  {    public void MethodThree()    {      Console.WriteLine(" SubSystemThree Method");    }  }  // Subsystem ClassD"   class SubSystemFour  {    public void MethodFour()    {      Console.WriteLine(" SubSystemFour Method");    }  }  // "Facade"   class Facade  {    SubSystemOne one;    SubSystemTwo two;    SubSystemThree three;    SubSystemFour four;    public Facade()    {      one = new SubSystemOne();      two = new SubSystemTwo();      three = new SubSystemThree();      four = new SubSystemFour();    }    public void MethodA()    {      Console.WriteLine("\nMethodA() ---- ");      one.MethodOne();      two.MethodTwo();      four.MethodFour();    }    public void MethodB()    {      Console.WriteLine("\nMethodB() ---- ");      two.MethodTwo();      three.MethodThree();    }  }}
參考資料
  • 1.    "The Facade design pattern - Structure and Collaboration". w3sDesign.com. Retrieved 2017-08-12.
  • 2.    "The Facade design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-12.