设计模式(17)-----外观设计模式

外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。

介绍

意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。

何时使用: 1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可。 2、定义系统的入口。

如何解决:客户端不与系统耦合,外观类与系统耦合。

关键代码:在客户端和复杂系统之间再加一层,这一层将调用顺序、依赖关系等处理好。

应用实例: 1、去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。 2、JAVA 的三层开发模式。

优点: 1、减少系统相互依赖。 2、提高灵活性。 3、提高了安全性。

缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。

使用场景: 1、为复杂的模块或子系统提供外界访问的模块。 2、子系统相对独立。 3、预防低水平人员带来的风险。

注意事项:在层次化结构中,可以使用外观模式定义系统中每一层的入口。

案例:

我们看到上面的一张图:

我们现在希望,按下一个按钮就可以看电影,而不是说要需要调用n多个对象去实现

下面我们来看代码:

PopcornPopper(爆米花类)

 1 package com.DesignPatterns.ag.hometheater;
 2 
 3 public class PopcornPopper {
 4     String description;
 5     
 6     public PopcornPopper(String description) {
 7         this.description = description;
 8     }
 9  
10     public void on() {
11         System.out.println(description + " on");
12     }
13  
14     public void off() {
15         System.out.println(description + " off");
16     }
17 
18     public void pop() {
19         System.out.println(description + " popping popcorn!");
20     }
21  
22   
23         public String toString() {
24                 return description;
25         }
26 }

TheaterLights(灯光类)

 1 package com.DesignPatterns.ag.hometheater;
 2 
 3 public class TheaterLights {
 4     String description;
 5     
 6     public TheaterLights(String description) {
 7         this.description = description;
 8     }
 9   
10     public void on() {
11         System.out.println(description + " on");
12     }
13   
14     public void off() {
15         System.out.println(description + " off");
16     }
17   
18     public void dim(int level) {
19         System.out.println(description + " dimming to " + level  + "%");
20     }
21    
22         public String toString() {
23                 return description;
24         }
25 }

Screen(投影机类)

 1 package com.DesignPatterns.ag.hometheater;
 2 
 3 public class Projector {
 4     String description;
 5     DvdPlayer dvdPlayer;
 6     
 7     public Projector(String description, DvdPlayer dvdPlayer) {
 8         this.description = description;
 9         this.dvdPlayer = dvdPlayer;
10     }
11  
12     public void on() {
13         System.out.println(description + " on");
14     }
15  
16     public void off() {
17         System.out.println(description + " off");
18     }
19 
20     public void wideScreenMode() {
21         System.out.println(description + " in widescreen mode (16x9 aspect ratio)");
22     }
23 
24     public void tvMode() {
25         System.out.println(description + " in tv mode (4x3 aspect ratio)");
26     }
27   
28         public String toString() {
29                 return description;
30         }
31 }

Amplifier(功放类)

 1 package com.DesignPatterns.ag.hometheater;
 2 
 3 public class Amplifier {
 4     String description;
 5     Tuner tuner;
 6     DvdPlayer dvd;
 7     CdPlayer cd;
 8     
 9     public Amplifier(String description) {
10         this.description = description;
11     }
12  
13     public void on() {
14         System.out.println(description + " on");
15     }
16  
17     public void off() {
18         System.out.println(description + " off");
19     }
20  
21     public void setStereoSound() {
22         System.out.println(description + " stereo mode on");
23     }
24  
25     public void setSurroundSound() {
26         System.out.println(description + " surround sound on (5 speakers, 1 subwoofer)");
27     }
28  
29     public void setVolume(int level) {
30         System.out.println(description + " setting volume to " + level);
31     }
32 
33     public void setTuner(Tuner tuner) {
34         System.out.println(description + " setting tuner to " + dvd);
35         this.tuner = tuner;
36     }
37   
38     public void setDvd(DvdPlayer dvd) {
39         System.out.println(description + " setting DVD player to " + dvd);
40         this.dvd = dvd;
41     }
42  
43     public void setCd(CdPlayer cd) {
44         System.out.println(description + " setting CD player to " + cd);
45         this.cd = cd;
46     }
47  
48     public String toString() {
49         return description;
50     }
51 }

DvdPlayer(dvd类)

 1 package com.DesignPatterns.ag.hometheater;
 2 
 3 public class DvdPlayer {
 4     String description;
 5     int currentTrack;
 6     Amplifier amplifier;
 7     String movie;
 8     
 9     public DvdPlayer(String description, Amplifier amplifier) {
10         this.description = description;
11         this.amplifier = amplifier;
12     }
13  
14     public void on() {
15         System.out.println(description + " on");
16     }
17  
18     public void off() {
19         System.out.println(description + " off");
20     }
21 
22         public void eject() {
23         movie = null;
24                 System.out.println(description + " eject");
25         }
26  
27     public void play(String movie) {
28         this.movie = movie;
29         currentTrack = 0;
30         System.out.println(description + " playing \"" + movie + "\"");
31     }
32 
33     public void play(int track) {
34         if (movie == null) {
35             System.out.println(description + " can't play track " + track + " no dvd inserted");
36         } else {
37             currentTrack = track;
38             System.out.println(description + " playing track " + currentTrack + " of \"" + movie + "\"");
39         }
40     }
41 
42     public void stop() {
43         currentTrack = 0;
44         System.out.println(description + " stopped \"" + movie + "\"");
45     }
46  
47     public void pause() {
48         System.out.println(description + " paused \"" + movie + "\"");
49     }
50 
51     public void setTwoChannelAudio() {
52         System.out.println(description + " set two channel audio");
53     }
54  
55     public void setSurroundAudio() {
56         System.out.println(description + " set surround audio");
57     }
58  
59     public String toString() {
60         return description;
61     }
62 }

测试类

 1 package com.DesignPatterns.ag.hometheater;
 2 
 3 public class HomeTheaterTestDrive {
 4     public static void main(String[] args) {
 5         Amplifier amp = new Amplifier("Top-O-Line Amplifier");
 6         Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp);
 7         DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp);
 8         Projector projector = new Projector("Top-O-Line Projector", dvd);
 9         TheaterLights lights = new TheaterLights("Theater Ceiling Lights");
10         Screen screen = new Screen("Theater Screen");
11         PopcornPopper popper = new PopcornPopper("Popcorn Popper");
12  
13         HomeTheaterFacade homeTheater = 
14                 new HomeTheaterFacade(amp, tuner, dvd,  
15                         projector, screen, lights, popper);
16  
17         homeTheater.watchMovie("Raiders of the Lost Ark");
18         homeTheater.endMovie();
19     }
20 }
Get ready to watch a movie...
Popcorn Popper on
Popcorn Popper popping popcorn!
Theater Ceiling Lights dimming to 10%
Theater Screen going down
Top-O-Line Projector on
Top-O-Line Projector in widescreen mode (16x9 aspect ratio)
Top-O-Line Amplifier on
Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player
Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer)
Top-O-Line Amplifier setting volume to 5
Top-O-Line DVD Player on
Top-O-Line DVD Player playing "Raiders of the Lost Ark"
Shutting movie theater down...
Popcorn Popper off
Theater Ceiling Lights on
Theater Screen going up
Top-O-Line Projector off
Top-O-Line Amplifier off
Top-O-Line DVD Player stopped "Raiders of the Lost Ark"
Top-O-Line DVD Player eject
Top-O-Line DVD Player off

从上面我们可以看到一个watchMovie   endMovie就可以调用许多类的方法,这个就是典型的外观设计模式,把复杂的运算包装起来,只是留一个开关来控制后面一大堆的逻辑来实现的。 

猜你喜欢

转载自www.cnblogs.com/qingruihappy/p/9789016.html
今日推荐