资讯专栏INFORMATION COLUMN

开发之路(设计模式八:外观模式)

hosition / 3545人阅读

摘要:改变接口的新模式,为了简化接口这次带来的模式为外观模式,之所以这么称呼,因为它将一个或多个类复杂的一切都隐藏起来。

</>复制代码

  1. 改变接口的新模式,为了简化接口

这次带来的模式为外观模式,之所以这么称呼,因为它将一个或多个类复杂的一切都隐藏起来。

我依旧举生活中例子,现在有些朋友家的液晶电视可能是大尺寸的,或者有用投影仪来看电视,打游戏的。有一天我想用家庭影院系统在家里看一次大片。

</>复制代码

  1. 1、打开爆米花机
  2. 2、开始爆米花
  3. 3、将灯光调暗
  4. 4、放下屏幕
  5. 5、打开投影仪
  6. 6、将投影仪的输入切换到DVD
  7. 7、将投影仪设置在宽屏模式下
  8. 8、打开功放
  9. 9、将功放的输入设置为DVD
  10. 10、将功放的设置为环绕立体声
  11. 11、功放音量调到中(5)
  12. 12、打开DVD播放器
  13. 13、开始播放DVD

等我操作完这些我才能开始享受大片,需要打开这么多开关。。而且如果看完要关闭是不是又要反向把动作再来一遍,这样或许连我都没兴趣使用影院系统了。也许这套系统该升级了!!

这时候外观模式就发挥作用了,通过实现一个提供更合理的接口的外观类,让一个复杂的系统变得使用简单了,
我们来看看外观是怎么运作的。如图(可点击查看原图)

这里注意:
1、外观不只是简化了接口,也将客户从组件的子系统中解耦,若客户有需要还是可以使用子系统,外观是为了简化接口。
2、外观和适配器模式可以包装许多类,但是外观的意图是简化接口,而适配器的意图是将接口转换成不同的接口,换句话说是让两个原本不适用的东西,变得能相互使用一样。

现在开始我们的家庭影院系统升级把~~~~

家庭影院类(外观)

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * 家庭影院 (将各个组件统一的“接口”)
  4. *
  5. * @author Joy
  6. *
  7. */
  8. // 外观类
  9. public class HomeTheaterFacade {
  10. /**
  11. * 组合,将用到的子系统组件全部都在这里
  12. */
  13. Amplifier amp;// 扩音器
  14. Tuner tuner;// 无线电收音机
  15. DvdPlayer dvd;// DVD播放器
  16. CdPlayer cd;// CD播放器
  17. Projector projector;// 投影仪
  18. TheaterLights lights;// 灯光
  19. Screen screen;// 屏幕
  20. PopcornPopper popper;// 爆米花机
  21. // 外观将子系统中每个组件的引用都传入它的构造器中,
  22. // 然后外观把它们赋值给相应的实例
  23. public HomeTheaterFacade(Amplifier amp, Tuner tuner, DvdPlayer dvd,
  24. CdPlayer cd, Projector projector, Screen screen,
  25. TheaterLights lights, PopcornPopper popper) {
  26. this.amp = amp;
  27. this.tuner = tuner;
  28. this.dvd = dvd;
  29. this.cd = cd;
  30. this.projector = projector;
  31. this.screen = screen;
  32. this.lights = lights;
  33. this.popper = popper;
  34. }
  35. /**
  36. * 将子系统的组件整合成一个统一的接口 watchMovie()方法 就相当于把所有操作放在一个方法里,简化操作
  37. * 将我们之前手动进行的每项任务我依次处理。 注意:每项任务都是具体实现都是委托子系统中相应的组件处理的
  38. *
  39. * @param movie
  40. */
  41. public void watchMovie(String movie) {
  42. System.out.println("准备好,看电影了...");
  43. popper.on();
  44. popper.pop();
  45. lights.dim(10);
  46. screen.down();
  47. projector.on();
  48. projector.wideScreenMode();
  49. amp.on();
  50. amp.setDvd(dvd);
  51. amp.setSurroundSound();
  52. amp.setVolume(5);
  53. dvd.on();
  54. dvd.play(movie);
  55. }
  56. /**
  57. * endMovie() 负责关闭一切,每项任务也都是委托子系统中对应组件的具体方法
  58. */
  59. public void endMovie() {
  60. System.out.println("关闭电影...");
  61. popper.off();
  62. lights.on();
  63. screen.up();
  64. projector.off();
  65. amp.off();
  66. dvd.stop();
  67. dvd.eject();
  68. dvd.off();
  69. }
  70. /**
  71. * 下面这项方法,若客户还是需要调用子系统的方法话,相应调用即可
  72. *
  73. * @param cdTitle
  74. */
  75. public void listenToCd(String cdTitle) {
  76. System.out.println("Get ready for an audiopile experence...");
  77. lights.on();
  78. amp.on();
  79. amp.setVolume(5);
  80. amp.setCd(cd);
  81. amp.setStereoSound();
  82. cd.on();
  83. cd.play(cdTitle);
  84. }
  85. public void endCd() {
  86. System.out.println("Shutting down CD...");
  87. amp.off();
  88. amp.setCd(cd);
  89. cd.eject();
  90. cd.off();
  91. }
  92. public void listenToRadio(double frequency) {
  93. System.out.println("Tuning in the airwaves...");
  94. tuner.on();
  95. tuner.setFrequency(frequency);
  96. amp.on();
  97. amp.setVolume(5);
  98. amp.setTuner(tuner);
  99. }
  100. public void endRadio() {
  101. System.out.println("Shutting down the tuner...");
  102. tuner.off();
  103. amp.off();
  104. }
  105. }

下面是各个组件类(子系统)

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * 扩音器类
  4. *
  5. * @author Joy
  6. *
  7. */
  8. public class Amplifier {
  9. // 音响类型
  10. String description;
  11. Tuner tuner;
  12. DvdPlayer dvd;
  13. CdPlayer cd;
  14. public Amplifier(String description) {
  15. this.description = description;
  16. }
  17. public void on() {
  18. System.out.println(description + " 开启");
  19. }
  20. public void off() {
  21. System.out.println(description + " 关闭");
  22. }
  23. public void setStereoSound() {
  24. System.out.println(description + " 环绕立体模式");
  25. }
  26. public void setSurroundSound() {
  27. System.out.println(description + " 环绕声设置(5 音量, 1 低音炮)");
  28. }
  29. public void setVolume(int level) {
  30. System.out.println(description + " 设置音量: " + level);
  31. }
  32. public void setTuner(Tuner tuner) {
  33. System.out.println(description + "设置音效 " + dvd);
  34. this.tuner = tuner;
  35. }
  36. public void setDvd(DvdPlayer dvd) {
  37. System.out.println(description + " 设置DVD播放器 " + dvd);
  38. this.dvd = dvd;
  39. }
  40. public void setCd(CdPlayer cd) {
  41. System.out.println(description + " 设置CD播放器" + cd);
  42. this.cd = cd;
  43. }
  44. public String toString() {
  45. return description;
  46. }
  47. }

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * CD播放器
  4. *
  5. * @author Joy
  6. *
  7. */
  8. public class CdPlayer {
  9. String description;
  10. int currentTrack;
  11. Amplifier amplifier;
  12. String title;
  13. public CdPlayer(String description, Amplifier amplifier) {
  14. this.description = description;
  15. this.amplifier = amplifier;
  16. }
  17. public void on() {
  18. System.out.println(description + " 开启");
  19. }
  20. public void off() {
  21. System.out.println(description + " 关闭");
  22. }
  23. public void eject() {
  24. title = null;
  25. System.out.println(description + " 弹出");
  26. }
  27. public void play(String title) {
  28. this.title = title;
  29. currentTrack = 0;
  30. System.out.println(description + " 播放 "" + title + """);
  31. }
  32. public String toString() {
  33. return description;
  34. }
  35. }

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * DVD播放器
  4. *
  5. * @author Joy
  6. *
  7. */
  8. public class DvdPlayer {
  9. String description;
  10. int currentTrack;
  11. Amplifier amplifier;
  12. String movie;
  13. public DvdPlayer(String description, Amplifier amplifier) {
  14. this.description = description;
  15. this.amplifier = amplifier;
  16. }
  17. public void on() {
  18. System.out.println(description + " 开启");
  19. }
  20. public void off() {
  21. System.out.println(description + " 关闭");
  22. }
  23. public void eject() {
  24. movie = null;
  25. System.out.println(description + " 弹出");
  26. }
  27. public void play(String movie) {
  28. this.movie = movie;
  29. currentTrack = 0;
  30. System.out.println(description + " 播放 "" + movie + """);
  31. }
  32. public void play(int track) {
  33. if (movie == null) {
  34. System.out.println(description + " can"t play track " + track
  35. + " no dvd inserted");
  36. } else {
  37. currentTrack = track;
  38. System.out.println(description + " playing track " + currentTrack
  39. + " of "" + movie + """);
  40. }
  41. }
  42. public void stop() {
  43. currentTrack = 0;
  44. System.out.println(description + " 停止 "" + movie + """);
  45. }
  46. public String toString() {
  47. return description;
  48. }
  49. }

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * 爆米花机类
  4. *
  5. * @author Joy
  6. *
  7. */
  8. public class PopcornPopper {
  9. String description;
  10. public PopcornPopper(String description) {
  11. this.description = description;
  12. }
  13. public void on() {
  14. System.out.println(description + " 开启");
  15. }
  16. public void off() {
  17. System.out.println(description + " 关闭");
  18. }
  19. public void pop() {
  20. System.out.println(description + "正在工作!");
  21. }
  22. public String toString() {
  23. return description;
  24. }
  25. }

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * 投影仪类
  4. * @author Joy
  5. *
  6. */
  7. public class Projector {
  8. String description;
  9. DvdPlayer dvdPlayer;
  10. public Projector(String description, DvdPlayer dvdPlayer) {
  11. this.description = description;
  12. this.dvdPlayer = dvdPlayer;
  13. }
  14. public void on() {
  15. System.out.println(description + " 开启");
  16. }
  17. public void off() {
  18. System.out.println(description + " 关闭");
  19. }
  20. public void wideScreenMode() {
  21. System.out.println(description + " 设置成宽屏模式 (16x9 高宽比)");
  22. }
  23. public void tvMode() {
  24. System.out.println(description + " 设置为TV模式 (4x3 高宽比)");
  25. }
  26. public String toString() {
  27. return description;
  28. }
  29. }

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * 屏幕类
  4. *
  5. * @author Joy
  6. *
  7. */
  8. public class Screen {
  9. String description;
  10. public Screen(String description) {
  11. this.description = description;
  12. }
  13. public void up() {
  14. System.out.println(description + " 上升");
  15. }
  16. public void down() {
  17. System.out.println(description + " 降下");
  18. }
  19. public String toString() {
  20. return description;
  21. }
  22. }

</>复制代码

  1. package headfirst.facade.hometheater;
  2. /**
  3. * 影院灯光
  4. *
  5. * @author Joy
  6. *
  7. */
  8. public class TheaterLights {
  9. String description;
  10. public TheaterLights(String description) {
  11. this.description = description;
  12. }
  13. public void on() {
  14. System.out.println(description + " 打开");
  15. }
  16. public void off() {
  17. System.out.println(description + " 关闭");
  18. }
  19. public void dim(int level) {
  20. System.out.println(description + " 掉亮度为: " + level + "%");
  21. }
  22. public String toString() {
  23. return description;
  24. }
  25. }

</>复制代码

  1. package headfirst.facade.hometheater;
  2. public class Tuner {
  3. String description;
  4. Amplifier amplifier;
  5. double frequency;
  6. public Tuner(String description, Amplifier amplifier) {
  7. this.description = description;
  8. }
  9. public void on() {
  10. System.out.println(description + " on");
  11. }
  12. public void off() {
  13. System.out.println(description + " off");
  14. }
  15. public void setFrequency(double frequency) {
  16. System.out.println(description + " setting frequency to " + frequency);
  17. this.frequency = frequency;
  18. }
  19. public String toString() {
  20. return description;
  21. }
  22. }

测试类(用轻松方式看电影)

</>复制代码

  1. package TestMain;
  2. import headfirst.facade.hometheater.Amplifier;
  3. import headfirst.facade.hometheater.CdPlayer;
  4. import headfirst.facade.hometheater.DvdPlayer;
  5. import headfirst.facade.hometheater.HomeTheaterFacade;
  6. import headfirst.facade.hometheater.PopcornPopper;
  7. import headfirst.facade.hometheater.Projector;
  8. import headfirst.facade.hometheater.Screen;
  9. import headfirst.facade.hometheater.TheaterLights;
  10. import headfirst.facade.hometheater.Tuner;
  11. /**
  12. * 外观作用简化接口
  13. * 适配器作用是将接口能与另一个接口相匹配
  14. *
  15. * @author Joy
  16. *
  17. */
  18. public class HomeTheaterTestDrive {
  19. public static void main(String[] args) {
  20. // 将各个组件都实例化
  21. Amplifier amp = new Amplifier("Top-O-Line 扩音器");
  22. Tuner tuner = new Tuner("Top-O-Line AM/FM 调谐器", amp);
  23. DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD 播放器", amp);
  24. CdPlayer cd = new CdPlayer("Top-O-Line CD 播放器", amp);
  25. Projector projector = new Projector("Top-O-Line 投影仪", dvd);
  26. TheaterLights lights = new TheaterLights("影院天花板灯");
  27. Screen screen = new Screen("影院屏幕");
  28. PopcornPopper popper = new PopcornPopper("爆米花机");
  29. // 实例化外观
  30. HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, dvd,
  31. cd, projector, screen, lights, popper);
  32. // 使用简化的开启关闭接口
  33. homeTheater.watchMovie("《夺宝奇兵》");
  34. homeTheater.endMovie();
  35. }
  36. }

效果图

是不是感到很轻松了,我只需要“放入”我想看的电影,剩下一切程序自动化完成。

外观模式定义:提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。

再来一张图来理解外观

这里就引出一个“最少知识”原则
最少知识原则告诉我们要减少对象之间的交互,只留下几个“密友”,当你在设计一个系统的时候,,不管是任何对象,都要注意它所交互的类有哪些,并注意它和这些类是如何交互的。
这个原则希望我们设计中,不要让太多的类耦合在一起,免得修改系统一部分,就会影响到其他部分。如果许多类之间相互依赖,那么系统就会变得牵一发而动全身了也因为要复杂而不容易被人理解。

要点:
1、当需要简化并统一一个很大的接口或者一群复杂的接口时,使用外观。
2、外观将客户从一个复杂的子系统中解耦出来。
3、实现一个外观,将子系统组合委托给外观,然后外观将工作让子系统执行。

感谢你看到这里,外观模式到这里就结束了,本人文笔随便,若有不足或错误之处望给予指点,90度弯腰~~~很快我会发布下一个设计模式的内容,生命不息,编程不止!

</>复制代码

  1. 参考书籍:《Head First 设计模式》

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70457.html

相关文章

  • JavaScript设计模式系列外观模式

    摘要:外观模式外观模式是指提供一个统一的接口去访问多个子系统的多个不同的接口,为子系统中的一组接口提供统一的高层接口。外观模式在我们的日常工作中十分常见。 外观模式 外观模式是指提供一个统一的接口去访问多个子系统的多个不同的接口,为子系统中的一组接口提供统一的高层接口。使得子系统更容易使用,不仅简化类中的接口,而且实现调用者和接口的解耦。外观模式在我们的日常工作中十分常见。 我们来看一个例子...

    tianren124 评论0 收藏0
  • 设计模式(通往高手之路的必备技能)

    摘要:设计模式的定义在面向对象软件设计过程中针对特定问题的简洁而优雅的解决方案。从前由于使用的局限性,和做的应用相对简单,不被重视,就更谈不上设计模式的问题。 ‘从大处着眼,从小处着手’,以前对这句话一知半解,自从踏出校门走入社会,开始工作以来,有了越来越深的理解,偶有发现这句话用在程序开发中也有用,所以,近段时间开始尝试着分析jQuery源码,分析angularjs源码,学习设计模式。 设...

    paraller 评论0 收藏0
  • 前端进阶之路: 前端架构设计(1)-代码核心

    摘要:可能很多人和我一样首次听到前端架构这个词第一反应是前端还有架构这一说呢在后端开发领域系统规划和可扩展性非常关键因此架构师备受重视早在开发工作启动之前他们就被邀请加入到项目中而且他们会跟客户讨论即将建成的平台的架构要求使用还什么技术栈内容类型 可能很多人和我一样, 首次听到前端架构这个词, 第一反应是: 前端还有架构这一说呢? 在后端开发领域, 系统规划和可扩展性非常关键, 因此架构师备...

    DevYK 评论0 收藏0
  • 前端进阶之路: 前端架构设计(1)-代码核心

    摘要:可能很多人和我一样首次听到前端架构这个词第一反应是前端还有架构这一说呢在后端开发领域系统规划和可扩展性非常关键因此架构师备受重视早在开发工作启动之前他们就被邀请加入到项目中而且他们会跟客户讨论即将建成的平台的架构要求使用还什么技术栈内容类型 可能很多人和我一样, 首次听到前端架构这个词, 第一反应是: 前端还有架构这一说呢? 在后端开发领域, 系统规划和可扩展性非常关键, 因此架构师备...

    baishancloud 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<