外觀模式(Facade) ,也叫「流程模式:外觀模式為子系統中的一組介面提供一個一致的介面 ,此模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。
#外觀模式透過定義一個一致的接口,用以屏蔽內部子系統的細節,使得呼叫端只需跟這個介面發生呼叫,而無需關心這個子系統的內部細節。
#類別圖解析:
#Facade:為呼叫端提供統一的呼叫介面,外觀類別知道哪些子系統負責處理請求,從而將呼叫端的請求代理給適當子系統物件。
#Client:外觀介面的呼叫者。
SubSystem集合:指模組或子系統,處理Facade物件指派的任務,他是功能的實際提供者。
背景介紹:
組成家庭劇院:DVD播放器、投影機、自動螢幕、環繞立體聲、爆米花機,要求完成使用家庭劇院的功能,透過直接用遙控器(統籌各設備開關)進行控制,其過程為:
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class DVDPlayer {
// 使用单例模式
private static final DVDPlayer instance = new DVDPlayer();
private DVDPlayer() {}
public static DVDPlayer getInstance() {
return instance;
}
public void on() {
System.out.println("dvd 打开了...");
}
public void off() {
System.out.println("dvd 关闭了...");
}
public void play() {
System.out.println("dvd 播放中...");
}
public void pause() {
System.out.println("dvd 暂停了...");
}
}</pre><div class="contentsignin">登入後複製</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class Popcorn {
private static final Popcorn instance = new Popcorn();
private Popcorn(){}
public static Popcorn getInstance() {
return instance;
}
public void on() {
System.out.println("爆米花机打开了...");
}
public void off() {
System.out.println("爆米花机关闭了...");
}
public void pop() {
System.out.println("爆米花做好了...");
}
}</pre><div class="contentsignin">登入後複製</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class Projector {
private static final Projector instance = new Projector();
private Projector(){}
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println("投影仪打开了...");
}
public void off() {
System.out.println("投影仪关闭了...");
}
public void focus() {
System.out.println("投影仪聚焦中...");
}
}</pre><div class="contentsignin">登入後複製</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class Screen {
private static final Screen instance = new Screen();
private Screen(){}
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println("屏幕上升...");
}
public void down() {
System.out.println("屏幕下降...");
}
}</pre><div class="contentsignin">登入後複製</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class Stereo {
private static final Stereo instance = new Stereo();
private Stereo(){}
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println("立体音响打开...");
}
public void off() {
System.out.println("立体音响关闭...");
}
public void up() {
System.out.println("立体音响音量+...");
}
public void down() {
System.out.println("立体音响音量-...");
}
}</pre><div class="contentsignin">登入後複製</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class TheaterLight {
private static final TheaterLight instance = new TheaterLight();
private TheaterLight(){}
public static TheaterLight getInstance() {
return instance;
}
public void on() {
System.out.println("灯光打开...");
}
public void off() {
System.out.println("灯光关闭...");
}
public void dim() {
System.out.println("灯光亮度调暗...");
}
public void bright() {
System.out.println("灯光亮度调亮...");
}
}</pre><div class="contentsignin">登入後複製</div></div>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class HomeTheaterFacade {
private DVDPlayer dvdPlayer;
private Popcorn popcorn;
private Projector projector;
private Stereo stereo;
private Screen screen;
private TheaterLight theaterLight;
public HomeTheaterFacade() {
this.dvdPlayer = DVDPlayer.getInstance();
this.popcorn = Popcorn.getInstance();
this.projector = Projector.getInstance();
this.stereo = Stereo.getInstance();
this.screen = Screen.getInstance();
this.theaterLight = TheaterLight.getInstance();
}
/**
* 准备开始
*/
public void ready() {
popcorn.on();
popcorn.pop();
screen.down();
projector.on();
projector.focus();
stereo.on();
dvdPlayer.on();
theaterLight.dim();
}
/**
* 播放
*/
public void play() {
dvdPlayer.play();
}
/**
* 暂停
*/
public void pause() {
dvdPlayer.pause();
}
/**
* 结束
*/
public void end() {
popcorn.off();
theaterLight.bright();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}
}</pre><div class="contentsignin">登入後複製</div></div>
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
System.out.println("----------准备开始-----------");
homeTheaterFacade.ready();
System.out.println("----------开始播放-----------");
homeTheaterFacade.play();
System.out.println("----------播放暂停-----------");
homeTheaterFacade.pause();
System.out.println("----------播放结束-----------");
homeTheaterFacade.end();
}
}
外觀模式
對外屏蔽了子系統的細節透過合理的使用外觀模式,可以幫我們更好的
劃分存取的層次以上是Java設計模式:外觀模式實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!