一、

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.

 package headfirst.designpatterns.facade.hometheater;

 public class HomeTheaterFacade {
Amplifier amp;
Tuner tuner;
DvdPlayer dvd;
CdPlayer cd;
Projector projector;
TheaterLights lights;
Screen screen;
PopcornPopper popper; public HomeTheaterFacade(Amplifier amp,
Tuner tuner,
DvdPlayer dvd,
CdPlayer cd,
Projector projector,
Screen screen,
TheaterLights lights,
PopcornPopper popper) { this.amp = amp;
this.tuner = tuner;
this.dvd = dvd;
this.cd = cd;
this.projector = projector;
this.screen = screen;
this.lights = lights;
this.popper = popper;
} public void watchMovie(String movie) {
System.out.println("Get ready to watch a movie...");
popper.on();
popper.pop();
lights.dim(10);
screen.down();
projector.on();
projector.wideScreenMode();
amp.on();
amp.setDvd(dvd);
amp.setSurroundSound();
amp.setVolume(5);
dvd.on();
dvd.play(movie);
} public void endMovie() {
System.out.println("Shutting movie theater down...");
popper.off();
lights.on();
screen.up();
projector.off();
amp.off();
dvd.stop();
dvd.eject();
dvd.off();
} public void listenToCd(String cdTitle) {
System.out.println("Get ready for an audiopile experence...");
lights.on();
amp.on();
amp.setVolume(5);
amp.setCd(cd);
amp.setStereoSound();
cd.on();
cd.play(cdTitle);
} public void endCd() {
System.out.println("Shutting down CD...");
amp.off();
amp.setCd(cd);
cd.eject();
cd.off();
} public void listenToRadio(double frequency) {
System.out.println("Tuning in the airwaves...");
tuner.on();
tuner.setFrequency(frequency);
amp.on();
amp.setVolume(5);
amp.setTuner(tuner);
} public void endRadio() {
System.out.println("Shutting down the tuner...");
tuner.off();
amp.off();
}
}

2.

 package headfirst.designpatterns.facade.hometheater;

 public class HomeTheaterTestDrive {
public static void main(String[] args) {
Amplifier amp = new Amplifier("Top-O-Line Amplifier");
Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp);
DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp);
CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp);
Projector projector = new Projector("Top-O-Line Projector", dvd);
TheaterLights lights = new TheaterLights("Theater Ceiling Lights");
Screen screen = new Screen("Theater Screen");
PopcornPopper popper = new PopcornPopper("Popcorn Popper"); HomeTheaterFacade homeTheater =
new HomeTheaterFacade(amp, tuner, dvd, cd,
projector, screen, lights, popper); homeTheater.watchMovie("Raiders of the Lost Ark");
homeTheater.endMovie();
}
}

3.

 package headfirst.designpatterns.facade.hometheater;

 public class Amplifier {
String description;
Tuner tuner;
DvdPlayer dvd;
CdPlayer cd; public Amplifier(String description) {
this.description = description;
} public void on() {
System.out.println(description + " on");
} public void off() {
System.out.println(description + " off");
} public void setStereoSound() {
System.out.println(description + " stereo mode on");
} public void setSurroundSound() {
System.out.println(description + " surround sound on (5 speakers, 1 subwoofer)");
} public void setVolume(int level) {
System.out.println(description + " setting volume to " + level);
} public void setTuner(Tuner tuner) {
System.out.println(description + " setting tuner to " + dvd);
this.tuner = tuner;
} public void setDvd(DvdPlayer dvd) {
System.out.println(description + " setting DVD player to " + dvd);
this.dvd = dvd;
} public void setCd(CdPlayer cd) {
System.out.println(description + " setting CD player to " + cd);
this.cd = cd;
} public String toString() {
return description;
}
}

4.

 package headfirst.designpatterns.facade.hometheater;

 public class Tuner {
String description;
Amplifier amplifier;
double frequency; public Tuner(String description, Amplifier amplifier) {
this.description = description;
} public void on() {
System.out.println(description + " on");
} public void off() {
System.out.println(description + " off");
} public void setFrequency(double frequency) {
System.out.println(description + " setting frequency to " + frequency);
this.frequency = frequency;
} public void setAm() {
System.out.println(description + " setting AM mode");
} public void setFm() {
System.out.println(description + " setting FM mode");
} public String toString() {
return description;
}
}

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. windbg基本命令

    1, .reload k 当前调用堆栈.u 当前正在执行的代码. 2, ~ 查看被调试进程中的线程信息每一行是一个线程的信息.第一行中,0 表示这个进程的编号:1ff4.1038 是 16 进制数字, ...

  2. 一种简单的权限管理ER图设计

    权限管理支持动态地管理用户的角色和权限.权限代表用户可以在什么对象上进行什么操作:角色是一组权限的集合. PS:当增加或删除某个用户的角色时,系统自动将该角色对应的权限(角色 -权限关联表)增加或删除 ...

  3. Memcached的安装(Linux)、操作、命令

    最近在整理有关分布式缓存的服务器,做了一下老牌nosql服务器memcached的学习总结.文中所述的所有安装均是在联网的情况下进行的. 序: 什么是memcached: Free & ope ...

  4. Mac下使用Web服务器性能/压力测试工具webbench、ab、siege

    Web开发,少不了的就是压力测试,它是评估一个产品是否合格上线的基本标准,下面我们来一一剖析他们的使用方式. 测试前,前面先把系统的端口限制数改大,看看Mac下面的默认限制 ulimit -a ope ...

  5. mac 上的版本控制工具SmartSVN9.0.4(破解版)

    附带上破解版安装说明: 1.在MAC上选中SmartSVN.dmg,右键->打开2.双击syntevo_keygen.jar 如果没有安装java会自动提示安装的3.输入Name Email(随 ...

  6. Sublime字体设置

    {"font_face": "Courier New","font_options":["subpixel_antialias&q ...

  7. 编写高性能 Web 应用程序的 10 个技巧

    使用 ASP.NET 编写 Web 应用程序的简单程度令人不敢相信.正因为如此简单,所以很多开发人员就不会花时间来设计其应用程序的结构,以获得更好的性能了.在本文中,我将讲述 10 个用于编写高性能 ...

  8. Demo学习: ClientEvents

    ClientEvents 在控件的ClientEvents属性里嵌入JS代码,增加了开发的灵活性. 分别在TUniPanel和TUniTimer的 ClientEvents事件里添加了JS代码: 1. ...

  9. compared woth QPSK, what is the advantages of QAM(16QAM or 64QAM?)

    1.QPSK QPSK是英文Quadrature Phase Shift Keying的缩略语简称,意为正交相移键控,是一种数字调制方式.在数字信号的调制方式中QPSK四相移键控是目前最常用的一种卫星 ...

  10. IEEE 754 浮点数的四种舍入方式

    四种舍入方向: 向最接近的可表示的值:当有两个最接近的可表示的值时首选"偶数"值:向负无穷大(向下):向正无穷大(向上)以及向0(截断). 说明:默认模式是最近舍入(Round t ...