1。当需要使用一个现有的类而其接口并不符合你的需求时,就使用适配器。
2。当需要简化并统一一个个很大的接口或者一群复杂的接口时,就使用外观。
3。适配器改变接口以符合客户的期望。
4。外观将客户从一个复杂的子系统中解耦。
5。实现一个适配器可能需要一番功夫,也可能不费功夫,视目标接口的大小与复杂度而定。
6。实现一个外观,需要将子系统组合进外观众,然后将工作委托给子系统执行。
7。适配器模式有两种形式:对象适配器和类适配器。类适配器需要用到多重继承。
8。你可以为一个子系统实现一个以上的外观。
9。适配器将一个对象包装起来以改变其接口:装饰者将一个对象包装起来以增加新的行为和责任,而外观将一群对象包装起来以便简化其接口。
package pattern; public class HomeTheaterFacade { Amplifler amp = new Amplifler(); Tuner tuner = new Tuner(); DvdPlayer dvd = new DvdPlayer(); CdPlayer cdPlayer = new CdPlayer(); Projector projector = new Projector(); TheaterLights lights = new TheaterLights(); PopcomPopper popper = new PopcomPopper(); Screen screen = new Screen(); public HomeTheaterFacade(Amplifler amp, Tuner tuner, DvdPlayer dvd, CdPlayer cdPlayer, Projector projector, TheaterLights lights, PopcomPopper popper, Screen screen) { amp = amp; tuner = tuner; dvd = dvd; cdPlayer = cdPlayer; projector = projector; lights = lights; popper = popper; screen = screen; } 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.setSurroundSoud(); amp.setVolume(5); dvd.on(); dvd.play(movie); } public void EndMovie(String movie) { System.out.println("Shutting movie theater down ...."); popper.off(); lights.on(); screen.up(); projector.off(); amp.off(); dvd.stop(); dvd.eject(); } } class Amplifler { Tuner tuner; DvdPlayer dvdPlayer; CdPlayer cdPlayer; public void on() { System.out.println(" Amplifler on"); } public void off() { System.out.println(" Amplifler off"); } public void setCd(CdPlayer cdPlayer) { cdPlayer = cdPlayer; } public void setDvd(DvdPlayer dvdPlayer) { dvdPlayer = dvdPlayer; } public void setSurroundSoud() { System.out.println("Amplifler setSurroundSoud"); } public void setTuner(Tuner tuner) { tuner = tuner; } public void setVolume(int vol) { System.out.println("Amplifler setVolume " + vol); } public String toString() { return "Amplifler"; } } class Tuner { public void on() { System.out.println(" Tuner on"); } public void off() { System.out.println(" Tuner off"); } public void setAm() { System.out.println("Tuner setAm"); } public void setFm() { System.out.println("Tuner setFm"); } public void setFrequency() { System.out.println("Tuner setFrequency"); } public String toString() { return "Tuner"; } } class DvdPlayer { public void on() { System.out.println(" DvdPlayer on"); } public void off() { System.out.println(" DvdPlayer off"); } public void eject() { System.out.println(" DvdPlayer eject"); } public void pause() { System.out.println(" DvdPlayer pause"); } public void play(String movie) { System.out.println(" DvdPlayer play " + movie); } public void setSurroundAudio() { System.out.println(" DvdPlayer setSurroundAudio"); } public void setTwoChannelAudio() { System.out.println(" DvdPlayer setTwoChannelAudio"); } public void stop() { System.out.println(" DvdPlayer stop"); } } class Screen { public void up() { System.out.println(" Screen up"); } public void down() { System.out.println(" Screen down"); } public String toString() { return "screen"; } } class CdPlayer { public void on() { System.out.println(" CdPlayer on"); } public void off() { System.out.println(" CdPlayer off"); } public void eject() { System.out.println(" CdPlayer eject"); } public void pause() { System.out.println(" CdPlayer pause"); } public void play() { System.out.println(" CdPlayer play"); } public void stop() { System.out.println(" CdPlayer stop"); } public String toString() { return "CdPlayer"; } } class Projector { DvdPlayer dvdPlayer; public void on() { System.out.println(" Projector on"); } public void off() { System.out.println(" Projector off"); } public void tvMode() { System.out.println(" Projector tvMode"); } public void wideScreenMode() { System.out.println(" Projector wideScreenMode"); } public String toString() { return "Projector"; } } class PopcomPopper { public void on() { System.out.println(" PopcomPopper on"); } public void off() { System.out.println(" PopcomPopper off"); } public void pop() { System.out.println(" PopcomPopper pop"); } public String toString() { return "PopcomPopper"; } } class TheaterLights { public void on() { System.out.println(" TheaterLights on"); } public void off() { System.out.println(" TheaterLights off"); } public void dim(int dim) { System.out.println(" TheaterLights dim " + dim); } public String toString() { return "TheaterLights"; } } package pattern; public class HomeTheaterTestDrive { /** * @param args */ public static void main(String[] args) { Amplifler amp = new Amplifler(); Tuner tuner = new Tuner(); DvdPlayer dvd = new DvdPlayer(); CdPlayer cdPlayer = new CdPlayer(); Projector projector = new Projector(); TheaterLights lights = new TheaterLights(); PopcomPopper popper = new PopcomPopper(); Screen screen = new Screen(); HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, dvd, cdPlayer, projector, lights, popper, screen); homeTheater.WatchMovie("TMD"); homeTheater.EndMovie("TMD"); } }