外观模式(Facade)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用
为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口
UMl类图(原理)
UML类图(案例)
代码实现
public class DVDPlayer { // DVD播放器 使用饿汉 public static final DVDPlayer DVD_PLAYER = new DVDPlayer(); private DVDPlayer() { } public static DVDPlayer getInstance() { return DVD_PLAYER; } public void on() { System.out.println("dvd on"); } public void off() { System.out.println("dvd off"); } public void play() { System.out.println("dvd play"); } public void pause() { System.out.println("dvd pause"); } }
public class Screen { // 屏幕 使用饿汉 public static final Screen SCREEN = new Screen(); private Screen() { } public static Screen getInstance() { return SCREEN; } public void up() { System.out.println("screen up"); } public void dowm() { System.out.println("screen down"); } }
public class Popcorn { // 爆米花机 使用饿汉 public static final Popcorn POPCORN = new Popcorn(); private Popcorn() { } public static Popcorn getInstance() { return POPCORN; } public void on() { System.out.println("popcorn on"); } public void off() { System.out.println("popcorn off"); } public void pop() { System.out.println("popcorn pop"); } }
public class Projector { // 投影仪 使用饿汉 public static final Projector PROJECTOR = new Projector(); private Projector() { } public static Projector getInstance() { return PROJECTOR; } public void on() { System.out.println("projector on"); } public void off() { System.out.println("projector off"); } public void focus() { System.out.println("projector focus"); } }
public class TheaterLight { // 影院灯光 使用饿汉 public static final TheaterLight THEATER_LIGHT = new TheaterLight(); private TheaterLight() { } public static TheaterLight getInstance() { return THEATER_LIGHT; } public void on() { System.out.println("theater_light on"); } public void off() { System.out.println("theater_light off"); } public void dim() { System.out.println("theater_light dim"); } public void bright() { System.out.println("theater_light bright"); } }
public class Stereo { // 立体声 使用饿汉 public static final Stereo STEREO = new Stereo(); private Stereo() { } public static Stereo getInstance() { return STEREO; } public void on() { System.out.println("stereo on"); } public void off() { System.out.println("stereo off"); } public void up() { System.out.println("stereo up"); } }
public class HomeTheaterFacade { // 影院外观类,聚合其他子系统 ,提供统一的方法供Client调用 private DVDPlayer dvdPlayer = DVDPlayer.getInstance(); private Screen screen = Screen.getInstance(); private Popcorn popcorn = Popcorn.getInstance(); private Projector projector = Projector.getInstance(); private TheaterLight theaterLight = TheaterLight.getInstance(); private Stereo stereo = Stereo.getInstance(); public HomeTheaterFacade() { } // 给Client提供的方法相当于子系统方法的集合 public void ready() { popcorn.on(); popcorn.pop(); screen.dowm(); projector.on(); projector.focus(); theaterLight.on(); stereo.on(); dvdPlayer.on(); } public void play() { theaterLight.dim(); stereo.up(); dvdPlayer.play(); } public void pause() { dvdPlayer.pause(); } public void end() { popcorn.off(); screen.up(); projector.off(); theaterLight.off(); stereo.off(); dvdPlayer.off(); } }
public class Client { public static void main(String[] args) { HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(); System.out.println("-----------ready-----------"); homeTheaterFacade.ready(); System.out.println("-----------play-----------"); homeTheaterFacade.play(); System.out.println("-----------pause-----------"); homeTheaterFacade.pause(); System.out.println("-----------end-----------"); homeTheaterFacade.end(); } }
原文:https://www.cnblogs.com/xiaokantianse/p/14007432.html