本文地址: http://blog.csdn.net/caroline_wendy
外观模式(facade pattern): 提供了一个统一的接口, 用来访问子系统中的一群接口. 外观定义了一个高层接口, 让子系统更容易使用.
外观模式包含三个部分:
1. 子系统: 子类, 单个复杂子类 或 多个子类;
2. 外观(facade)类: 把子系统设计的更加容易使用;
3. 客户: 只需要调用外观类.
与适配器模式(adapter pattern)的区别:
适配器模式的意图是, "改变"接口符合客户的期望; 外观模式的意图是, 提供子系统的一个简化接口.
面向对象的设计原则:
最少知识原则: 只和你的密友交谈.
具体方法:
1. 外观(facade)类, 包含多个子类的组合, 和一些调用多个子类的接口:
package facade; 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, TheaterLights lights, Screen screen, PopcornPopper popper) { // TODO Auto-generated constructor stub this.amp = amp; this.tuner = tuner; this.dvd = dvd; this.cd = cd; this.projector = projector; this.lights = lights; this.screen = screen; 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 threater down..."); popper.off(); lights.on(); screen.up(); projector.off(); amp.off(); dvd.stop(); dvd.eject(); dvd.off(); } }
/** * @time 2014年6月17日 */ package facade; /** * @author C.L.Wang * */ public class HomeTheaterTestDrive { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub 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("Theatera Ceiling Lights"); Screen screen = new Screen("Theater Screen"); PopcornPopper popper = new PopcornPopper("Popcorn Popper"); HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, dvd, cd, projector, lights, screen, popper); homeTheater.watchMovie("Who is a girl? "); homeTheater.endMovie(); } }
Get ready to watch a movie... Popcorn Popper on Popcorn Popper popping popcorn! Theater Ceiling Lights dimming to 10% Theater Screen going down Top-O-Line Projector on Top-O-Line Projector in widescreen mode (16x9 aspect ratio) Top-O-Line Amplifier on Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer) Top-O-Line Amplifier setting volume to 5 Top-O-Line DVD Player on Top-O-Line DVD Player playing "Who is a girl? " Shutting movie threater down... Popcorn Popper off Theater Ceiling Lights on Theater Screen going up Top-O-Line Projector off Top-O-Line Amplifier off Top-O-Line DVD Player stopped "Who is a girl? " Top-O-Line DVD Player eject Top-O-Line DVD Player off
4. 子类其余代码下载: http://download.csdn.net/detail/u012515223/7511683
设计模式 - 外观模式(facade pattern) 详解,布布扣,bubuko.com
设计模式 - 外观模式(facade pattern) 详解
原文:http://blog.csdn.net/caroline_wendy/article/details/31782225