設計模式_門面模式

Facade Pattern
    Provide a unified interface to a set of interfaces in a subsystem.Facade defines a higher-level interface that makes the subsystem easier to use.(要求一個子系統的外部與其內部的通信必須通過一個統一的對象進行,門面模式提供一個高層次的接口,使得子系統更易於使用)

爲什麼這也算一種設計模式? 

public interface ILetterProcess {
 public void writerContext(String context);
 public void fillEnvelope(String address);
 public void letterIntoEnvelope();
 public void sendLetter();
}

public class LetterProcess implements ILetterProcess {
 public void fillEnvelope(String address) {
  System.out.println("填寫信的地址:"+address);
 }
 public void letterIntoEnvelope() {
  System.out.println("把信放進信封");
 }
 public void sendLetter() {
  System.out.println("郵遞信件");
 }
 public void writerContext(String context) {
  System.out.println("寫信內容:"+context);
 }
}

public class ModenPostOffice {
 private ILetterProcess letterProcess=new LetterProcess();
 
 public void sendLetter(String context,String address){
  this.letterProcess.writerContext(context);
  this.letterProcess.fillEnvelope(address);
  this.letterProcess.letterIntoEnvelope();
  this.letterProcess.sendLetter();
 }
}

public class Client {
 
 public static void main(String[] args) {
  ModenPostOffice postOffice=new ModenPostOffice();
  postOffice.sendLetter("你被錄取了", "XX大學");
 }
}

優點 
    靈活,封裝,安全,減少依賴

缺點
    不符合開閉原則,沒辦法擴展

使用場景
    爲一個複雜子系統或者模塊提供一個外界訪問的接口
    子系統相對獨立
    預防一個低水平的技術人員參與項目開發,爲降低個人代碼質量對整體項目的影響風險

我是菜鳥,我在路上。
發佈了78 篇原創文章 · 獲贊 17 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章