package facade;
/**
* @author jiq
* 型別:Structural
* 定義:外觀模式(Facade)提供一個統一的介面,用來訪問子系統中的一群介面。外觀
* 定義了一個高層介面,使得子系統更容易使用。
*
* OO原則:最少知識原則 --- 只和你的密友交談。
*
* 區別: 裝飾器模式包裝一個物件,並提供額外的行為。
* 介面卡模式包裝一個物件,並轉換成不同的介面(介面改變)。
* 外觀模式包裝許多物件,以簡化他們的介面(介面改變)。
* 代理模式包裝一個物件,並控制它的訪問(不改變被代理物件的介面)。
*/
//螢幕
class Screen {
String description;
public Screen(String description) {
this.description = description;
}
public void up() { /*...*/ }
public void down() { /*...*/ }
}
//DVD播放器
class DvdPlayer {
String description;
int currentTrack;
Projector projector;
String title;
public DvdPlayer(String description, Projector projector) {
this.description = description;
this.projector = projector;
}
public void on() { /*...*/ }
public void off() { /*...*/ }
public void eject() { /*...*/ }
public void play(String title) { /*...*/ }
public void stop() { /*...*/ }
public void pause() { /*...*/ }
}
//投影儀
class Projector {
String description;
DvdPlayer cd;
public Projector(String description) {
this.description = description;
}
public void on() { /*...*/ }
public void off() { /*...*/ }
public void setStereoSound() { /*...*/ }
public void setSurroundSound() { /*...*/ }
public void setVolume(int level) { /*...*/ }
public void setCd(DvdPlayer cd) { /*...*/ }
}
//家庭影院(外觀)
class HomeTheaterFacade {
Projector projector;
DvdPlayer cd;
Screen screen;
public HomeTheaterFacade(Projector amp,DvdPlayer cd,Screen screen) {
this.projector = amp;
this.cd = cd;
this.screen = screen;
}
//開始看電影(將一群介面封裝為一個簡潔的介面)
public void watchMovie(String movie) {
System.out.println("Get ready to watch a movie...");
screen.down();
projector.on();
projector.setSurroundSound();
projector.setVolume(5);
}
//結束看電影(將一群介面封裝為一個簡潔的介面)
public void endMovie() {
System.out.println("Shutting movie theater down...");
screen.up();
projector.off();
}
}
public class FacadeTest {
public static void main(String[] args) {
Projector projector = new Projector("Top-O-Line Amplifier");
DvdPlayer cd = new DvdPlayer("Top-O-Line CD Player",projector);
Screen screen = new Screen("Theater Screen");
HomeTheaterFacade homeTheater = new HomeTheaterFacade(projector,cd,screen);
homeTheater.watchMovie("Raiders of the Lost Ark");
homeTheater.endMovie();
}
}