一、
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 49 public void endMovie() { 50 System.out.println("Shutting movie theater down..."); 51 popper.off(); 52 lights.on(); 53 screen.up(); 54 projector.off(); 55 amp.off(); 56 dvd.stop(); 57 dvd.eject(); 58 dvd.off(); 59 } 60 61 public void listenToCd(String cdTitle) { 62 System.out.println("Get ready for an audiopile experence..."); 63 lights.on(); 64 amp.on(); 65 amp.setVolume(5); 66 amp.setCd(cd); 67 amp.setStereoSound(); 68 cd.on(); 69 cd.play(cdTitle); 70 } 71 72 public void endCd() { 73 System.out.println("Shutting down CD..."); 74 amp.off(); 75 amp.setCd(cd); 76 cd.eject(); 77 cd.off(); 78 } 79 80 public void listenToRadio(double frequency) { 81 System.out.println("Tuning in the airwaves..."); 82 tuner.on(); 83 tuner.setFrequency(frequency); 84 amp.on(); 85 amp.setVolume(5); 86 amp.setTuner(tuner); 87 } 88 89 public void endRadio() { 90 System.out.println("Shutting down the tuner..."); 91 tuner.off(); 92 amp.off(); 93 } 94 }
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 }