一、传统方式解决影院管理说明

  1、外观模式可以理解为转换一群接口,客户只要调用一个接口,而不用调用多个接口才能达到目的。比如:在 PC 删安装软件软件的时候经常有一键安装选项(省去选择安装目录、安装的组件等),还有就是手机的重启功能(把关机和启动合为一个操作)。

  2、外观模式就是解决多个复杂接口带来的使用困难,起到简化用户操作的作用;

  3、示意图说明:

  

二、外观模式应用实例

  1、家庭影院要求

  2、使用外观模式来完成家庭影院项目

  3、思路分析和图解(类图)

    

  4、代码实现

    各个设备类:

  1 public class DVDPlayer {
  2 
  3     //使用单例模,饿汉式
  4     private static DVDPlayer instance = new DVDPlayer();
  5 
  6     private DVDPlayer() {}
  7 
  8     public static DVDPlayer getInstance() {
  9         return instance;
 10     }
 11 
 12     public void on() {
 13         System.out.println("DVD 打开");
 14     }
 15 
 16     public void off() {
 17         System.out.println("DVD 关闭");
 18     }
 19 
 20     public void play() {
 21         System.out.println("DVD 正在播放");
 22     }
 23 
 24     public void pause() {
 25         System.out.println("DVD 暂定");
 26     }
 27 
 28 }
 29 ---------------------------------------------------
 30 /**
 31  * 爆米花机
 32  */
 33 public class Popcorn {
 34     private static Popcorn instance = new Popcorn();
 35 
 36     private Popcorn() {}
 37 
 38     public static Popcorn getInstance() {
 39         return instance;
 40     }
 41 
 42     public void on() {
 43         System.out.println("打开爆米花机");
 44     }
 45 
 46     public void off() {
 47         System.out.println("关闭爆米花机");
 48     }
 49 
 50     public void pop() {
 51         System.out.println("爆米花ing");
 52     }
 53 
 54 }
 55 ---------------------------------------------------
 56 /**
 57  * 投影仪
 58  */
 59 public class Projector {
 60     private static Projector instance = new Projector();
 61 
 62     private Projector() {}
 63 
 64     public static Projector getInstance() {
 65         return instance;
 66     }
 67 
 68     public void on() {
 69         System.out.println("打开投影仪");
 70     }
 71 
 72     public void off() {
 73         System.out.println("关闭投影仪");
 74     }
 75 
 76     public void focus() {
 77         System.out.println("投影仪聚焦");
 78     }
 79 }
 80 ---------------------------------------------------
 81 /**
 82  * 屏幕
 83  */
 84 public class Screen {
 85 
 86     private static Screen instance = new Screen();
 87 
 88     private Screen() {}
 89 
 90     public static Screen getInstance() {
 91         return instance;
 92     }
 93 
 94     public void down() {
 95         System.out.println("放下屏幕");
 96     }
 97 
 98     public void up() {
 99         System.out.println("收起屏幕");
100     }
101 
102 }
103 ---------------------------------------------------
104 /**
105  * 立体声
106  */
107 public class Stereo {
108     private static Stereo instance = new Stereo();
109 
110     private Stereo() {}
111 
112     public static Stereo getInstance() {
113         return instance;
114     }
115 
116     public void on() {
117         System.out.println("打开音响");
118     }
119 
120     public void off() {
121         System.out.println("关闭音响");
122     }
123     public void up() {
124         System.out.println("调高音量");
125     }
126 
127     public void down() {
128         System.out.println("调低音量");
129     }
130 
131 }
132 ---------------------------------------------------
133 /**
134  * 灯光
135  */
136 public class TheaterLight {
137     private static TheaterLight instance = new TheaterLight();
138 
139     private TheaterLight() {}
140 
141     public static TheaterLight getInstance() {
142         return instance;
143     }
144 
145     public void on() {
146         System.out.println("打开灯光");
147     }
148 
149     public void off() {
150         System.out.println("关闭灯光");
151     }
152     public void dim() {
153         System.out.println("调暗灯光");
154     }
155     public void bright() {
156         System.out.println("调亮灯光");
157     }
158 }
159 ---------------------------------------------------

 

  家庭影院外观类:

 1 /**
 2  * 家庭影院
 3  */
 4 public class HomeTheaterFacade {
 5 
 6     //定义各个子系统对象
 7     private TheaterLight theaterLight; //灯光
 8     private Popcorn popcorn; //爆米花机
 9     private Stereo stereo; //音响
10     private Projector projector; //投影仪
11     private Screen screen;   //屏幕
12     private DVDPlayer dvdPlayer; //dvd
13 
14     //构造器
15     public HomeTheaterFacade() {
16         this.theaterLight = TheaterLight.getInstance();
17         this.popcorn = Popcorn.getInstance();
18         this.stereo = Stereo.getInstance();
19         this.projector = Projector.getInstance();
20         this.screen = Screen.getInstance();
21         this.dvdPlayer = DVDPlayer.getInstance();
22     }
23 
24     //操作分成4步
25     public void ready() {
26         popcorn.on();
27         popcorn.pop();
28         screen.down();
29         projector.on();
30         stereo.on();
31         dvdPlayer.on();
32         theaterLight.on();
33         theaterLight.dim();
34     }
35 
36     public void play() {
37         dvdPlayer.play();
38     }
39 
40     public void pause() {
41         dvdPlayer.pause();
42     }
43 
44     public void end() {
45         popcorn.off();
46         theaterLight.bright();
47         screen.up();
48         projector.off();
49         stereo.off();
50         dvdPlayer.off();
51     }
52 
53 }

 

  客户端:

 1 public class Client {
 2     public static void main(String[] args) {
 3         //直接调用各个设备,很麻烦,不利于扩展
 4 
 5 
 6         //统一调用
 7         HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
 8         homeTheaterFacade.ready();
 9         homeTheaterFacade.play();
10 
11         System.out.println("----------");
12         homeTheaterFacade.pause();
13         System.out.println("----------");
14         homeTheaterFacade.end();
15 
16     }
17 }

 

posted on 2021-01-27 22:39  格物致知_Tony  阅读(191)  评论(0编辑  收藏  举报