一、
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;
}
}