影院例子
package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:21 */ public class DVDPlay { private static DVDPlay instance = new DVDPlay(); private DVDPlay() { } public static DVDPlay getInstance(){ return instance; } public void on(){ System.out.println("on"); } public void off(){ System.out.println("off"); } public void play(){ System.out.println("play"); } public void pause(){ System.out.println("pause"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:23 */ public class BaoMiHua { private BaoMiHua(){} private static BaoMiHua instance = new BaoMiHua(); public static BaoMiHua getInstance(){ return instance; } public void on(){ System.out.println("爆米花机on"); } public void off(){ System.out.println("爆米花机off"); } public void pop(){ System.out.println("爆米花机pop"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:29 */ public class Light { private Light(){} private static Light instance = new Light(); public static Light getInstance(){ return instance; } public void on(){ System.out.println("灯光on"); } public void off(){ System.out.println("灯光off"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:27 */ public class Screen { private Screen(){} private static Screen instance = new Screen(); public static Screen getInstance(){ return instance; } public void up(){ System.out.println("屏幕up"); } public void down(){ System.out.println("屏幕down"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:25 */ public class TouYing { private TouYing(){} private static TouYing instance = new TouYing(); public static TouYing getInstance(){ return instance; } public void on(){ System.out.println("投影仪on"); } public void off(){ System.out.println("投影仪off"); } public void focus(){ System.out.println("投影仪focus"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:28 */ public class YinXiang { private YinXiang(){} private static YinXiang instance = new YinXiang(); public static YinXiang getInstance(){ return instance; } public void on(){ System.out.println("音响on"); } public void off(){ System.out.println("音响off"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:33 */ public class Home { //定义子系统对象 private BaoMiHua baoMiHua; private DVDPlay dvdPlay; private Light light; private Screen screen; private TouYing touYing; private YinXiang yinXiang; public Home() { this.baoMiHua = BaoMiHua.getInstance(); this.dvdPlay = DVDPlay.getInstance(); this.light = Light.getInstance(); this.screen = Screen.getInstance(); this.touYing = TouYing.getInstance(); this.yinXiang = YinXiang.getInstance(); } public void ready(){ baoMiHua.on(); baoMiHua.pop(); screen.down(); touYing.on(); yinXiang.on(); dvdPlay.on(); } public void play(){ dvdPlay.play(); } public void pause(){ dvdPlay.pause(); } public void end(){ baoMiHua.off(); screen.up(); touYing.off(); yinXiang.off(); dvdPlay.off(); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:30 */ public class Client { public static void main(String[] args) { Home home = new Home(); home.ready(); home.play(); } }灵魂在于设计分层 ——沃兹基朔德