java設計模式之面板模式學習筆記_java

來源:互聯網
上載者:User

面板模式: 又稱門面模式: 外觀Facade為子系統的一組介面提供一個一致介面,使得這組子系統便於使用(通過引入一個新的外觀角色降低原系統複雜度,同時降低客戶類與子系統的耦合度).

圖片來源: 設計模式: 可複用物件導向軟體的基礎.

實現

案例需求: 租房

有過自己找房租房經曆的同學能夠體會得到找房是件很痛苦的事, 不光要挨個小區跑而且還要跟(二)房東討價還價. 於是後來學聰明了, 不再自己挨門挨戶的磨嘴皮子, 而是直接找像鏈家、我愛我家這樣的房屋中介, 他們手上握有一定的房源, 我們只需付給他們一筆傭金, 他們便可以代我們跟房東講價, 而且他們大都很專業, 省時間又省錢. 此時房屋中介就是一個外觀Facade, 而房屋的出租戶就是子系統SubSystem:

Facade 
 外觀類: 知道哪些子系統負責處理請求, 將客戶的請求代理給適當的子系統對象:

public class MediumFacade {  private CuiYuanApartment cuiyuan;  private XiXiApartment xixi;  private XiHuApartment xihu;  public MediumFacade() {    cuiyuan = new CuiYuanApartment("翠苑小區", 900, 1);    xixi = new XiXiApartment("西溪花園", 1200, 1);    xihu = new XiHuApartment("西湖小區", 2600, 1);  }  public void rentingHouse(double price) {    // 價錢合適而且有房可組    if (price >= cuiyuan.getPrice() && cuiyuan.getStatus() != 0) {      System.out.println("預訂" + cuiyuan.getLocation());      cuiyuan.setStatus(0);    } else if (price >= xixi.getPrice() && xixi.getStatus() != 0) {      System.out.println("預訂" + xixi.getLocation());      xixi.setStatus(0);    } else if (price >= xihu.getPrice() && xihu.getStatus() != 0) {      System.out.println("預訂" + xihu.getLocation());      xihu.setStatus(0);    } else {      System.out.println("出價太低/沒有房源 ...");    }  }}

SubSystem 
 子系統集合: 實現子系統功能, 處理Facade對象指派的任務(注意子系統內沒有任何Facade資訊,即沒有任何Facade對象引用):

/** * @author jifang * @since 16/8/23 上午10:12. */public class XiHuApartment {  private String location;  private double price;  private int status;  public XiHuApartment(String location, double price, int status) {    this.location = location;    this.price = price;    this.status = status;  }  public String getLocation() {    return location;  }  public double getPrice() {    return price;  }  public int getStatus() {    return status;  }  public void setStatus(int status) {    this.status = status;  }}class XiXiApartment {  private String location;  private double price;  private int status;  public XiXiApartment(String location, double price, int status) {    this.location = location;    this.price = price;    this.status = status;  }  public String getLocation() {    return location;  }  public double getPrice() {    return price;  }  public int getStatus() {    return status;  }  public void setStatus(int status) {    this.status = status;  }}class CuiYuanApartment {  private String location;  private double price;  private int status;  public CuiYuanApartment(String location, double price, int status) {    this.location = location;    this.price = price;    this.status = status;  }  public String getLocation() {    return location;  }  public double getPrice() {    return price;  }  public int getStatus() {    return status;  }  public void setStatus(int status) {    this.status = status;  }}

Client
這樣, Client只需跟一個房屋中介聯絡並給出我們的報價, 他們便會幫我們聯絡所有符合的房東:

public class Client {  @Test  public void client() {    MediumFacade facade = new MediumFacade();    facade.rentingHouse(800);  }}

小結

有過物件導向開發經驗的同學 即使沒有聽說過面板模式, 也完全有可能使用過他, 因為他完美的體現了依賴倒轉原則和迪米特法則的思想, 是非常常用的模式之一.

使用 

 

首先 在設計初期, 應該有意識的進行層次分離, 比如經典的三層架構, 層與層之間建立Facade, 這樣可以為複雜的子系統提供一個簡單的介面, 使耦合度大大降低.

其次 在開發階段, 子系統往往因為不斷的重構而變得越來越複雜, 增加Facade可以提供一個簡單的介面, 減少模組間依賴.

第三 在維護一個遺留系統時, 可能這個系統已經非常難以維護和擴充了, 但因為它包含非常重要的功能, 新的需求必須依賴它, 此時可以為新系統開發一個Facade, 為設計粗糙或高複雜度的遺留代碼提供一個的比較清晰簡單的介面, 讓新系統與Facade互動, 而Facade與遺留代碼互動所有繁雜的工作.

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.