hooyantsing's Blog

P83 外观模式(3)-影院管理

字数统计: 599阅读时长: 3 min
2020/11/11

尚硅谷Java设计模式(图解+框架源码剖析)

外观模式 Facade

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

1
2
3
4
5
6
7
8
9
10
classDiagram
class 外观类
class 子系统1
class 子系统2
class 子系统3
class Client
外观类 o-- 子系统1
外观类 o-- 子系统2
外观类 o-- 子系统3
外观类 <.. Client
应用场景

有很多子系统提供功能,而上层调用时是根据不同阶段来调用。比如预先准备放电影,就会将打开DVD,制作爆米花,打开投影仪,将荧幕升上去等等。这样就可以把子系统在这一阶段提供的功能封装成一个方法,方便上层调用。

案例 影院管理

子系统类,这里使用单例饿汉模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// DVD
public class DVDPlayer {
private static DVDPlayer instance = new DVDPlayer();

public static DVDPlayer getInstance(){
return instance;
}

public void on(){
System.out.println("dvd on");
}

public void off(){
System.out.println("dvd off");
}

public void play(){
System.out.println("dvd playing");
}

public void pause(){
System.out.println("dvd pause");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 爆米花
public class Popcorn {
private static Popcorn instance = new Popcorn();

public static Popcorn getInstance(){
return instance;
}

public void on(){
System.out.println("popcorn on");
}

public void off(){
System.out.println("popcorn off");
}

public void pop(){
System.out.println("popcorn pop");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 投影仪
public class Projector {
private static Projector instance = new Projector();

public static Projector getInstance(){
return instance;
}

public void on(){
System.out.println("projector on");
}

public void off(){
System.out.println("projector off");
}

public void focus(){
System.out.println("projector focus");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Screen {
private static Screen instance = new Screen();

public static Screen getInstance(){
return instance;
}

public void up(){
System.out.println("screen up");
}

public void down(){
System.out.println("screen down");
}
}

以上子系统的功能,按照不同阶段使用的功能封装在这个类里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class HomeTheaterFacade {
private DVDPlayer dvdPlayer;
private Popcorn popcorn;
private Projector projector;
private Screen screen;

public HomeTheaterFacade(){
this.dvdPlayer = DVDPlayer.getInstance();
this.popcorn = Popcorn.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
}

public void ready(){
dvdPlayer.on();
popcorn.on();
projector.on();
screen.up();
}

public void play(){
dvdPlayer.play();
popcorn.pop();
projector.focus();
}

public void end(){
dvdPlayer.off();
popcorn.off();
projector.off();
screen.down();
}
}

调用测试:

1
2
3
4
5
6
7
8
9
10
11
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();

homeTheaterFacade.ready();
System.out.println("-----------------------------------");
homeTheaterFacade.play();
System.out.println("-----------------------------------");
homeTheaterFacade.end();
}
}

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
dvd on
popcorn on
projector on
screen up
-----------------------------------
dvd playing
popcorn pop
projector focus
-----------------------------------
dvd off
popcorn off
projector off
screen down
CATALOG
  1. 1. 外观模式 Facade
    1. 1.1. 应用场景
  2. 2. 案例 影院管理