前言
跟我们建造者不同的是,建造者模式的指挥者,通过聚合的方式调用其下面的方法后,在当前建造者中进行组合对外提供产品,而外观模式是对具体的类提供一个统一接口平台,不仅只是组合,更有多种不同的组合方式对外提供
提示:以下是本篇文章正文内容,下面案例可供参考
一、外观模式是什么?
基本介绍
-
外观模式(Facade),也叫“过程模式:外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
-
外观模式通过定义一个一致的接口 用以屏蔽内部子系统的细节,使得调用端 只需跟这个接口发生调用,而无需关心这个子系统的内部细节
二、使用步骤
1.定义外观类
public class HomeTheaterFacade {
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dvdPlayer;
public HomeTheaterFacade() {
this.popcorn = Popcorn.getInstance();
this.stereo = Stereo.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.dvdPlayer = DVDPlayer.getInstance();
}
public void ready(){
popcorn.on();
screen.down();
projector.on();
stereo.on();
dvdPlayer.on();
}
public void play(){
dvdPlayer.play();
}
public void pause(){
dvdPlayer.pause();
}
public void end(){
popcorn.off();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}
}
2.爆米花类
public class Popcorn {
private static Popcorn instance = new Popcorn();
public static Popcorn getInstance(){
return instance;
}
private Popcorn() {
}
public void on(){
System.out.println("买爆米花🍿");
}
public void off(){
System.out.println("爆米花吃完了🍿");
}
}
3.播放器类
public class DVDPlayer {
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstance(){
return instance;
}
private DVDPlayer() {
}
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播放器暂停📺");
}
}
4.屏幕类
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance(){
return instance;
}
private Screen() {
}
public void up(){
System.out.println("屏幕开始上升🖥️");
}
public void down(){
System.out.println("屏幕开始下降🖥️");
}
}
5.摄像机类
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance(){
return instance;
}
private Projector() {
}
public void on(){
System.out.println("投影仪打开📽️");
}
public void off(){
System.out.println("投影仪关闭📽️");
}
public void focus(){
System.out.println("投影仪聚焦📽️");
}
}
6.音响类
public class Stereo {
private static Stereo instance = new Stereo();
public static Stereo getInstance(){
return instance;
}
private Stereo() {
}
public void on(){
System.out.println("音响打开🔈");
}
public void off(){
System.out.println("音响关闭🔇");
}
}
7.调用
public class Client {
public static void main(String[] args) {
HomeTheaterFacade theaterFacade = new HomeTheaterFacade();
theaterFacade.ready();
theaterFacade.play();
theaterFacade.end();
}
}
8.输出
总结
外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需要跟这个接口发生调用,而无需关心这个子系统的内部细节
外观模式的注意事项和细节
外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性
外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展
通过合理的使用外观模式,可以帮我们更好的划分访问的层次
当系统需要进行分层设计时,可以考虑使用Facade模式
在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统的比较清晰简单的接口,让新系统与Facade类交互,提高复用性
不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好。要以让系统有层次,利干维护为目的。