外观模式

案例

1
2
3
4
5
6
7
8
9
10
11
12
组建—家庭影院:
DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要求完成使用家庭影院的功能,其过程为:
·直接用遥控器:统筹各设备开关
·开爆米花机
·放下屏幕
·开投影仪。
·开音响
·开DVD,选dvd。
·去拿爆米花
·调陪灯光
·播放
·观影结束后,关闭各种设备

传统方式的解决方法

1
2
3
4
5
6
7
ClientTest {
public static void main(String args[]){
//1.创建相关对象
//2.调用创建的各个对象的一系列方法
//3.调用DVDPlayer对象的play方法
}
}

传统方式解决影院管理问题

  1. 在ClientTest的main方法中,创建各个子系统的对象,并直接去调用子系统(对象)
    相关方法,会造成调用过程混乱,没有清晰的过程
  2. 不利于在ClientTest 中,去维护对子系统的操作
  3. 解决思路:定义一个高层接口,给子系统中的一组接口提供一个一致的界面(比
    如在高层接口提供四个方法ready, play, pause,end ),用来访问子系统中的一群接口
  4. 也就是说就是通过定义一个一致的接口(界面类),用以屏蔽内部子系统的细节
    使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节=>外观模式

基本介绍

  • 外观模式( Facade),也叫“过程模式:外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
  • 外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节
image-20220107145144687
  1. 外观(Facade)角色:为多个子系统对外提供一个共同的接口。外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适兰子系统对象
  2. 子系统(Sub System)角色:实现系统的部分功能,客户可以通过外观角色访问它。
  3. 客户(Client)角色:通过一个外观角色访问各个子系统的功能。

解决影院问题

设计一系列类

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
public class DVDPlayer {

//单例模式,饿汉式
private static DVDPlayer instance = new DVDPlayer();

private 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 is playing");
}

public void pause(){
System.out.println("dvd is pause");
}
//...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Popcorn {
//单例模式,饿汉式
private static Popcorn instance = new Popcorn();

private 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 popping(){
System.out.println("popcorn is popping");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Projector {
//单例模式,饿汉式
private static Projector instance = new Projector();

private 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("popcorn is focus");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Screen {
//单例模式,饿汉式
private static Screen instance = new Screen();

private 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
public class Stereo {
//单例模式,饿汉式
private static Stereo instance = new Stereo();

private Stereo(){

}

public static Stereo getInstance(){
return instance;
}

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

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

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

}
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
public class TheaterLight {
//单例模式,饿汉式
private static TheaterLight instance = new TheaterLight();

private TheaterLight(){

}

public static TheaterLight getInstance(){
return instance;
}

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

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

public void dim(){
System.out.println("TheaterLight dim");
}

public void bright(){
System.out.println("TheaterLight bright");
}
}

外观类

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class HomeTheaterFacade {

//定义各个子系统对象
private TheaterLight theaterLight; //灯光
private Popcorn popcorn; //爆米花
private Stereo stereo; //立体声
private Projector projector; //投影仪
private Screen screen; //屏幕
private DVDPlayer dvdPlayer; //DVD

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

//操作分为4步
public void ready(){
popcorn.on();
popcorn.popping();
screen.down();
projector.on();
stereo.on();
dvdPlayer.on();
theaterLight.dim();
}

public void play(){
dvdPlayer.play();
}

public void pause(){
dvdPlayer.pause();
}

public void end(){
popcorn.off();
theaterLight.bright();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}

}
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();
homeTheaterFacade.play();
homeTheaterFacade.pause();
homeTheaterFacade.end();
}

}

注意事项

  • 外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性
  • 外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展通过合理的使用外观模式,可以帮我们更好的划分访问的层次
  • 当系统需要进行分层设计时,可以考虑使用Facade模式
  • 在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统的比较清晰简单的接口,让新系统与Facade类交互,提高复用性
  • 不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好。要以让系统有层次,利于维护为目的。