一、

1.The Facade Pattern provides a unifi ed interface to a set of interfaces in a subsytem. Facade defi nes a higher-level interface that makes the subsystem easier to use.

2.Facades and adapters may wrap multiple classes, but a facade’s intent is to simplify, while an adapter’s is to convert

the interface to something different.

3.

4.

二、

实现家庭影院

1.

  1. package headfirst.designpatterns.facade.hometheater;
  2.  
  3. public class HomeTheaterFacade {
  4. Amplifier amp;
  5. Tuner tuner;
  6. DvdPlayer dvd;
  7. CdPlayer cd;
  8. Projector projector;
  9. TheaterLights lights;
  10. Screen screen;
  11. PopcornPopper popper;
  12.  
  13. public HomeTheaterFacade(Amplifier amp,
  14. Tuner tuner,
  15. DvdPlayer dvd,
  16. CdPlayer cd,
  17. Projector projector,
  18. Screen screen,
  19. TheaterLights lights,
  20. PopcornPopper popper) {
  21.  
  22. this.amp = amp;
  23. this.tuner = tuner;
  24. this.dvd = dvd;
  25. this.cd = cd;
  26. this.projector = projector;
  27. this.screen = screen;
  28. this.lights = lights;
  29. this.popper = popper;
  30. }
  31.  
  32. public void watchMovie(String movie) {
  33. System.out.println("Get ready to watch a movie...");
  34. popper.on();
  35. popper.pop();
  36. lights.dim(10);
  37. screen.down();
  38. projector.on();
  39. projector.wideScreenMode();
  40. amp.on();
  41. amp.setDvd(dvd);
  42. amp.setSurroundSound();
  43. amp.setVolume(5);
  44. dvd.on();
  45. dvd.play(movie);
  46. }
  47.  
  48. public void endMovie() {
  49. System.out.println("Shutting movie theater down...");
  50. popper.off();
  51. lights.on();
  52. screen.up();
  53. projector.off();
  54. amp.off();
  55. dvd.stop();
  56. dvd.eject();
  57. dvd.off();
  58. }
  59.  
  60. public void listenToCd(String cdTitle) {
  61. System.out.println("Get ready for an audiopile experence...");
  62. lights.on();
  63. amp.on();
  64. amp.setVolume(5);
  65. amp.setCd(cd);
  66. amp.setStereoSound();
  67. cd.on();
  68. cd.play(cdTitle);
  69. }
  70.  
  71. public void endCd() {
  72. System.out.println("Shutting down CD...");
  73. amp.off();
  74. amp.setCd(cd);
  75. cd.eject();
  76. cd.off();
  77. }
  78.  
  79. public void listenToRadio(double frequency) {
  80. System.out.println("Tuning in the airwaves...");
  81. tuner.on();
  82. tuner.setFrequency(frequency);
  83. amp.on();
  84. amp.setVolume(5);
  85. amp.setTuner(tuner);
  86. }
  87.  
  88. public void endRadio() {
  89. System.out.println("Shutting down the tuner...");
  90. tuner.off();
  91. amp.off();
  92. }
  93. }

2.

  1. package headfirst.designpatterns.facade.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. CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp);
  9. Projector projector = new Projector("Top-O-Line Projector", dvd);
  10. TheaterLights lights = new TheaterLights("Theater Ceiling Lights");
  11. Screen screen = new Screen("Theater Screen");
  12. PopcornPopper popper = new PopcornPopper("Popcorn Popper");
  13.  
  14. HomeTheaterFacade homeTheater =
  15. new HomeTheaterFacade(amp, tuner, dvd, cd,
  16. projector, screen, lights, popper);
  17.  
  18. homeTheater.watchMovie("Raiders of the Lost Ark");
  19. homeTheater.endMovie();
  20. }
  21. }

3.

  1. package headfirst.designpatterns.facade.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. }

4.

  1. package headfirst.designpatterns.facade.hometheater;
  2.  
  3. public class Tuner {
  4. String description;
  5. Amplifier amplifier;
  6. double frequency;
  7.  
  8. public Tuner(String description, Amplifier amplifier) {
  9. this.description = description;
  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 setFrequency(double frequency) {
  21. System.out.println(description + " setting frequency to " + frequency);
  22. this.frequency = frequency;
  23. }
  24.  
  25. public void setAm() {
  26. System.out.println(description + " setting AM mode");
  27. }
  28.  
  29. public void setFm() {
  30. System.out.println(description + " setting FM mode");
  31. }
  32.  
  33. public String toString() {
  34. return description;
  35. }
  36. }

HeadFirst设计模式之门面模式的更多相关文章

  1. python 设计模式之门面模式

    facade:建筑物的表面 门面模式是一个软件工程设计模式,主要用于面向对象编程. 一个门面可以看作是为大段代码提供简单接口的对象,就像类库.   门面模式被归入建筑设计模式.门面模式隐藏系统内部的细 ...

  2. 设计模式_Facade_门面模式

    形象例子: 我有一个专业的Nikon相机,我就喜欢自己手动调光圈.快门,这样照出来的照片才专业,但MM可不懂这些,教了半天也不会.幸好相机有Facade设计模式,把相机调整到自动档,只要对准目标按快门 ...

  3. JavaScript设计模式(6)-门面模式

    门面模式 门面模式(Facade Pattern):他隐藏了系统的复杂性,并向客户端提供了一个可以访问系统的接口.这种类型的设计模式属于结构性模式.为子系统中的一组接口提供了一个统一的访问接口,这个接 ...

  4. python设计模式之门面模式

    一.结构型设计模式 门面模式与单例模式,工厂模式不同,它是一种结构型模式. 结构型模式描述如何将对象和类组合成更大的结构 结构型模式是一种能够简化设计工作的模式,它能找出更简单的方法来认识或表示实体之 ...

  5. java设计模式之门面模式以及在java中作用

    门面模式在Tomcat中有多处使用,在Request和Response对象封装,从ApplicationContext到ServletContext封装中都用到了这种设计模式. 一个系统可以有几个门面 ...

  6. 【php设计模式】门面模式

    门面模式又叫外观模式,用来隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口.这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性. 这种模式涉及到一个单一的类 ...

  7. JAVA设计模式之门面模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述门面(Facade)模式的: 门面模式是对象的结构模式,外部与一个子系统的通信必须通过一个统一的门面对象进行.门面模式提供一个高层次的接口 ...

  8. JS设计模式——10.门面模式

    门面模式 这是一种组织性的模式,它可以用来修改类和对象的接口,使其更便于使用.它可以让程序员过得更轻松,使他们的代码变得更容易管理. 门面模式有两个作用: 简化类的接口 消除与使用她的客户代码之间的耦 ...

  9. JAVA设计模式之门面模式(外观模式)

    医院的例子 现代的软件系统都是比较复杂的,设计师处理复杂系统的一个常见方法便是将其“分而治之”,把一个系统划分为几个较小的子系统.如果把医院作为一个子系统,按照部门职能,这个系统可以划分为挂号.门诊. ...

随机推荐

  1. 关于ajax解析

    出处:http://www.cnblogs.com/huashanlin/archive/2006/10/09/524707.html 要很好地领会Ajax技术的关键是了解超文本传输协议(HTTP), ...

  2. Js中的运算符

    运算符 运算符:就是可以运算的符号 比如 + .-.*./ 运算符包括: 算术运算符 比较运算符 逻辑运算符 赋值运算符 字符串运算符 1.算术运算符 +.-.*./.%(求余数).++.-- ++: ...

  3. java访问webservice服务(二)

    欢迎转载,出处http://www.cnblogs.com/shizhongtao/p/3433679.html 利用cxf的框架实现 import javax.xml.namespace.QName ...

  4. 考研路之C语言

    今天在学习C的时候,又学到了一些新的内容,所以赶紧记录下来. case 1: #include <stdio.h> union exa{ struct{ int a; int b; }ou ...

  5. SPI协议及其工作原理详解

    一.概述. SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控 ...

  6. Django配置静态文件(CSS\js)及Django调用JS、CSS、图片等静态文件

    1 新建一项目: root@python:django-admin.py startproject csstest root@python:cd csstest root@python:ls csst ...

  7. unity3d KeyCode各键值说明

    KeyCode :KeyCode是由Event.keyCode返回的.这些直接映射到键盘上的物理键. http://docs.unity3d.com/ScriptReference/KeyCode.h ...

  8. win7下以兼容模式安装oracle10g

    在win7系统装Oracle时经常会遇到一个“Oracle 10g 出现程序异常终止,发生内部错误!请将以下文件提供给 Oracle技术部门“未知”“未知”“未知””这样一个错误,百度了下,才知道原来 ...

  9. Solr + Hadoop = Big Data Love

    FROM:http://architects.dzone.com/articles/solr-hadoop-big-data-love 许多人使用Hadoop的开源项目来处理大数据的大数据集,因为它是 ...

  10. 基于Vuforia的Hololens图像识别

    微软官方Hololens开发文档中有关于Vuforia的内容,https://developer.microsoft.com/en-us/windows/holographic/getting_sta ...