基本介紹
- 外觀模式(Facade),也叫“過程模式:外觀模式爲子系統中的一組接口提供一個一致的界面,此模式定義了一個高層接口,這個接口使得這一子系統更加容易使用
- 外觀模式通過定義一個一致的接口,用以屏蔽內部子系統的細節,使得調用端只需跟這個接口發生調用,而無需關心這個子系統的內部細節
原理類圖
類圖說明
- 外觀類(Facade): 爲調用端提供統一的調用接口, 外觀類知道哪些子系統負責處理請求,從而將調用端的請求代理給適當子系統對象
- 調用者(Client): 外觀接口的調用者
- 子系統的集合:指模塊或者子系統,處理 Facade 對象指派的任務,他是功能的實際提供者
- 示意圖說明
應用實例
以家庭影院操作爲例應用外觀模式
子系統
//DVD類
public class DVDPlayer {
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstance() {
return instance;
}
public void on() {
System.out.println(" dvd on ..");
}
public void off() {
System.out.println(" dvd off ..");
}
public void play() {
System.out.println(" dvd play ..");
}
public void pause() {
System.out.println(" dvd pause ..");
}
public void setDVD() {
System.out.println(" dvd set ..");
}
}
//投影儀類
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println(" Projector on ..");
}
public void off() {
System.out.println(" Projector off ..");
}
public void focus() {
System.out.println(" Projector focus ..");
}
}
//幕布類
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println(" Screen up ..");
}
public void down() {
System.out.println(" Screen down..");
}
}
//燈光類
public class TheaterLight {
private static TheaterLight instance = new TheaterLight();
public static TheaterLight getInstance() {
return instance;
}
public void on() {
System.out.println(" TheaterLight on ..");
}
public void off() {
System.out.println(" TheaterLight off ..");
}
public void dim() {
System.out.println(" TheaterLight dim ..");
}
public void bright() {
System.out.println(" TheaterLight bright ..");
}
}
外觀類(Facade)
//外觀類,提供統一的調用接口
public class HomeTheaterFacade {
//定義各個子系統對象
private TheaterLight theaterLight;
private Projector projector;
private Screen screen;
private DVDPlayer dVDPlayer;
//構造器
public HomeTheaterFacade() {
this.theaterLight = TheaterLight.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.dVDPlayer = DVDPlayer.getInstance();
}
public void ready() {
dVDPlayer.on();
theaterLight.on();
screen.down();//投影儀下降
projector.on();
}
public void play() {
dVDPlayer.play();
}
public void pause() {
dVDPlayer.pause();
}
public void end() {
dVDPlayer.off();
theaterLight.off();
screen.up();
projector.off();
}
}
調用者(Client)
//用戶端使用
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade= new HomeTheaterFacade();
homeTheaterFacade.ready();
System.out.println("=====================================");
homeTheaterFacade.play();
System.out.println("=====================================");
homeTheaterFacade.end();
}
}
外觀模式的注意事項和細節
- 外觀模式對外屏蔽了子系統的細節,因此外觀模式降低了客戶端對子系統使用的複雜性
- 外觀模式對客戶端與子系統的耦合關係 - 解耦,讓子系統內部的模塊更易維護和擴展
- 通過合理的使用外觀模式,可以幫我們更好的劃分訪問的層次
- 當系統需要進行分層設計時,可以考慮使用 Facade 模式
- 在維護一個遺留的大型系統時,可能這個系統已經變得非常難以維護和擴展,此時可以考慮爲新系統開發一個
Facade 類,來提供遺留系統的比較清晰簡單的接口,讓新系統與 Facade 類交互,提高複用性 - 不能過多的或者不合理的使用外觀模式,使用外觀模式好,還是直接調用模塊好。要以讓系統有層次,利於維護爲目的。