http://www.books.com.tw/products/0010750585
chap11.
表象(外觀)模式Facade
目的:用一個介面包裝各個子系統,由介面與客戶端做溝通。
package com.ssc24.chap11.demo01; public abstract class Electronics { private boolean power = false; public void powerOn() { this.power = true; } public void powerOff() { this.power = false; } public boolean isPowerOn() { return this.power; } protected void showStatus() { if (this.power) { System.out.println(this.getClass().getSimpleName() + " 運作中"); } else { System.out.println(this.getClass().getSimpleName() + " 電源未開啟"); } } }
package com.ssc24.chap11.demo01; public class KTVsystem extends Electronics { private String song; public void selectSong(String song) { this.song = song; } public void playSong() { System.out.println(this.getClass().getSimpleName() + " 播放 " + this.song); } }
package com.ssc24.chap11.demo01; public class PlayStation3 extends Electronics { private String cd; public void putCd(String cd) { this.cd = cd; } public String getCd() { return this.cd; } public void play() { System.out.println(this.getClass().getSimpleName() + " 開始播放 " + this.cd); } @Override public void showStatus() { super.showStatus(); if(isPowerOn()) { System.out.println(this.getClass().getSimpleName() + " 目前放入cd:" + this.cd); } } }
package com.ssc24.chap11.demo01; public class Stereo extends Electronics { private int sound = 50; public void setSound(int sound) { this.sound = sound; } public int getSound() { return this.sound; } @Override public void showStatus() { super.showStatus(); if (isPowerOn()) { System.out.println(this.getClass().getSimpleName() + " 音量為:" + this.sound); } } }
package com.ssc24.chap11.demo01; public class Television extends Electronics { private int sound = 50; private String source = "tvBox"; private int channel = 9; public void setSound(int sound) { this.sound = sound; } public void switchSource(String source) { this.source = source; } public void switchChannel(int channel) { this.channel = channel; } public void showTV() { System.out.println("目前觀看的頻道是: " + this.channel); } @Override public void showStatus() { super.showStatus(); if (isPowerOn()) { System.out.print(this.getClass().getSimpleName() + " 音量為:" + this.sound); if ("tvBox".equals(this.source)) { System.out.println(" ,頻道:" + this.channel); } if ("ktv".equals(this.source)) { System.out.println(" ,ktv播放中:" + this.channel); } if ("ps".equals(this.source)) { System.out.println(" ,ps畫面顯示中:" + this.channel); } } } public int getSound() { return this.sound; } public int getSource() { return this.getSource(); } public int getChannel() { return this.channel; } }
package com.ssc24.chap11.demo01; public class VideoRoomFacade { Television tv = new Television(); Stereo stereo = new Stereo(); PlayStation3 ps = new PlayStation3(); KTVsystem ktv = new KTVsystem(); public void readyPlayMovie(String cd) { this.stereo.powerOn(); this.tv.powerOn(); this.tv.setSound(50); this.tv.switchSource("ps"); ps.powerOn(); ps.putCd(cd); } public void playMovie() { if (ps.isPowerOn()) { ps.play(); } } public void showTV() { this.tv.showTV(); } public void turnOffAll() { this.stereo.powerOff(); this.ktv.powerOff(); this.ps.powerOff(); this.tv.powerOff(); } public void watchTv() { this.tv.powerOn(); this.tv.switchSource("tvBox"); } public void SwitchChannel(int channel) { this.tv.switchChannel(channel); } public void readyKTV() { this.stereo.powerOn(); this.ktv.powerOn(); this.tv.powerOn(); this.tv.setSound(50); this.tv.switchSource("ktv"); } public void selectSong(String song) { if(this.ktv.isPowerOn()) { this.ktv.selectSong(song); } } public void playSong() { if(this.ktv.isPowerOn()) { this.ktv.playSong(); } } public void setSound(int soundLevel) { if (this.tv.isPowerOn()) { this.tv.setSound(soundLevel); } if(this.stereo.isPowerOn()) { this.stereo.setSound(soundLevel); } } public void showAllStatus() { this.tv.showStatus(); this.stereo.showStatus(); this.ps.showStatus(); this.ktv.showStatus(); } }
/** ===外觀模式測試=== ---看電影--- PlayStation3 開始播放 Life of Pi Television 運作中 Television 音量為:50 ,ps畫面顯示中:9 Stereo 運作中 Stereo 音量為:50 PlayStation3 運作中 PlayStation3 目前放入cd:Life of Pi KTVsystem 電源未開啟 ---關機器--- Television 電源未開啟 Stereo 電源未開啟 PlayStation3 電源未開啟 KTVsystem 電源未開啟 ---看電視--- 目前觀看的頻道是: 9 目前觀看的頻道是: 20 Television 電源未開啟 Stereo 電源未開啟 PlayStation3 電源未開啟 KTVsystem 電源未開啟 ---唱KTV--- KTVsystem 播放 Moon Television 運作中 Television 音量為:50 ,ktv播放中:20 Stereo 運作中 Stereo 音量為:50 PlayStation3 電源未開啟 KTVsystem 運作中 Television 電源未開啟 Stereo 電源未開啟 PlayStation3 電源未開啟 KTVsystem 電源未開啟 **/