共计 3106 个字符,预计需要花费 8 分钟才能阅读完成。
外观模式
结构型模式
外观模式(Facade Pattern)暗藏零碎的复杂性,并向客户端提供了一个客户端能够拜访零碎的接口。它向现有的零碎增加一个接口,来暗藏零碎的复杂性。
这种模式波及到一个繁多的类,该类提供了客户端申请的简化办法和对现有零碎类办法的委托调用。
介绍
用意: 为子系统中的一组接口提供一个统一的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易应用。
次要解决: 升高拜访简单零碎的外部子系统时的复杂度,简化客户端与之的接口。
何时应用: 1、客户端不须要晓得零碎外部的简单分割,整个零碎只需提供一个 ” 接待员 ” 即可。2、定义零碎的入口。
如何解决: 客户端不与零碎耦合,外观类与零碎耦合。
要害代码: 在客户端和简单零碎之间再加一层,这一层将调用程序、依赖关系等解决好。
具体实现
第一步:创立各个电影院设施
public class DVDPlayer {private DVDPlayer() { } | |
private static DVDPlayer instance = new DVDPlayer(); | |
public static DVDPlayer getInstance() {return instance;} | |
public void on() {System.out.println("DVD 关上了"); | |
} | |
public void off() {System.out.println("DVD 敞开了"); | |
} | |
public void play() {System.out.println("DVD 播放了"); | |
} | |
public void pause() {System.out.println("DVD 暂停了"); | |
} | |
} |
public class Popcorn {private Popcorn() { } | |
private static Popcorn instance = new Popcorn(); | |
public static Popcorn getInstance() {return instance;} | |
public void on(){System.out.println("爆米花机关上了"); | |
} | |
public void off(){System.out.println("爆米花机关闭了"); | |
} | |
public void pop(){System.out.println("爆米花机开始工作了"); | |
} | |
public void pause(){System.out.println("爆米花机暂停了"); | |
} | |
} |
public class Projector {private Projector() { } | |
private static Projector instance = new Projector(); | |
public static Projector getInstance() {return instance;} | |
public void on() {System.out.println("投影仪关上了"); | |
} | |
public void off() {System.out.println("投影仪敞开了"); | |
} | |
public void play() {System.out.println("投影仪播放了"); | |
} | |
public void pause() {System.out.println("投影仪暂停了"); | |
} | |
} |
public class Screen {private Screen() { } | |
private static Screen instance = new Screen(); | |
public static Screen getInstance() {return instance;} | |
public void up() {System.out.println("屏幕回升了"); | |
} | |
public void down() {System.out.println("屏幕降落了"); | |
} | |
} |
public class Stereo {private Stereo() { } | |
private static Stereo instance = new Stereo(); | |
public static Stereo getInstance() {return instance;} | |
public void on() {System.out.println("立体声关上了"); | |
} | |
public void off() {System.out.println("平面敞开了"); | |
} | |
} |
public class TheatreLight {private TheatreLight() { } | |
private static TheatreLight instance = new TheatreLight(); | |
public static TheatreLight getInstance() {return instance;} | |
public void on() {System.out.println("灯光关上了"); | |
} | |
public void off() {System.out.println("灯光敞开了"); | |
} | |
} |
第二步:创立一个外观类
public class HomeTheatreFacade { | |
// 定义各个子系统的对象 | |
private TheatreLight theatreLight; | |
private Popcorn popcorn; | |
private Projector projector; | |
private DVDPlayer dvdPlayer; | |
private Screen screen; | |
private Stereo stereo; | |
public HomeTheatreFacade() {this.theatreLight = TheatreLight.getInstance(); | |
this.popcorn = Popcorn.getInstance(); | |
this.projector = Projector.getInstance(); | |
this.dvdPlayer = DVDPlayer.getInstance(); | |
this.screen = Screen.getInstance(); | |
this.stereo = Stereo.getInstance();} | |
// 操作分为 4 步 | |
public void ready() {popcorn.on(); | |
popcorn.pop(); | |
screen.down(); | |
projector.on(); | |
stereo.on(); | |
dvdPlayer.on(); | |
theatreLight.off();} | |
public void play(){dvdPlayer.play(); | |
} | |
public void pause(){dvdPlayer.pause(); | |
} | |
public void end(){popcorn.off(); | |
screen.up(); | |
theatreLight.on(); | |
projector.off(); | |
stereo.off(); | |
dvdPlayer.off();} | |
} |
第三步:创立测试
public class Client {public static void main(String[] args){HomeTheatreFacade homeTheatreFacade = new HomeTheatreFacade(); | |
homeTheatreFacade.ready(); | |
homeTheatreFacade.play(); | |
homeTheatreFacade.end();} | |
} |
运行如下:
爆米花机关上了 | |
爆米花机开始工作了 | |
屏幕降落了 | |
投影仪关上了 | |
立体声关上了 | |
DVD 关上了 | |
灯光敞开了 | |
DVD 播放了 | |
爆米花机关闭了 | |
屏幕回升了 | |
灯光关上了 | |
投影仪敞开了 | |
平面敞开了 | |
DVD 敞开了 |
长处:
1、缩小零碎相互依赖。2、进步灵活性。3、进步了安全性。
毛病:
不合乎开闭准则,如果要改货色很麻烦,继承重写都不适合。
正文完