适配器模式: 将一个类的接口,转换成客户所期待的接口,适配器让原本不兼容的类可以合作无间。有两种形式:类适配器和对象适配器。前者需要用到多重继承(java不支持),后者要用到组合。
外观模式: 提供了一个统一的简化的接口,用来访问子系统里的一群接口。外观定义了一个高层接口,让子系统更容易使用。
适配器的类图
适配器的一个简单源码:
package adapter; /** * 鸭子接口 * @author Arvon * */ public interface Duck { public void quack(); public void fly(); }
package adapter; /** * 火鸡接口 * @author Administrator * */ public interface Turkey { public void fly(); public void goggle(); }
package adapter; /** * 一个伪装成火鸡的鸭 * @author Administrator * */ public class TurkeyAdapter implements Duck { private Turkey turkey; public TurkeyAdapter(Turkey turkey) { super(); this.turkey = turkey; } @Override public void quack() { // TODO Auto-generated method stub turkey.goggle(); } @Override public void fly() { // TODO Auto-generated method stub for(int i=0;i<5;i++) turkey.fly(); } }
package adapter; public class MalladDuck implements Duck { @Override public void quack() { // TODO Auto-generated method stub System.out.println("i am quacking..."); } @Override public void fly() { // TODO Auto-generated method stub System.out.println("i am flying ..."); } }
package adapter; public class WildTurkey implements Turkey { @Override public void fly() { // TODO Auto-generated method stub System.out.println("i am flying short distance..."); } @Override public void goggle() { // TODO Auto-generated method stub System.out.println("i am goggling...."); } }
package adapter; public class AdapterTest { public static void main(String[] args) { // TODO Auto-generated method stub Duck duck = new MalladDuck(); System.out.println(duck.getClass().getSimpleName()+" says..."); testDuck(duck); //将鸭子接口转换成了火鸡接口 Duck fakeDuck = new TurkeyAdapter(new WildTurkey()); System.out.println(fakeDuck.getClass().getSimpleName()+" says..."); testDuck(fakeDuck); } private static void testDuck(Duck duck) { duck.quack(); duck.fly(); } }
程序输出:
MalladDuck says...
i am quacking...
i am flying ...
TurkeyAdapter says...
i am goggling....
i am flying short distance...
i am flying short distance...
i am flying short distance...
i am flying short distance...
i am flying short distance...
外观模式类图
核心代码:
package facade; /** * 简化了的接口 * @author Administrator * */ 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) { super(); 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 theater down..."); popper.off(); lights.on(); screen.up(); projector.off(); amp.off(); dvd.stop(); dvd.eject(); dvd.off(); } }
package facade; public class FacadeTest { 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("Theater 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("Raiders of the Lost Ark"); 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 "Raiders of the Lost Ark"
Shutting movie theater 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 "Raiders of the Lost Ark"
Top-O-Line DVD Player eject
Top-O-Line DVD Player off
版权声明:本文为博主原创文章,未经博主允许不得转载。