java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java外观模式

Java设计模式中的外观模式详解

作者:爱学习的大鱼​​​​​​​

这篇文章主要介绍了Java设计模式中的外观模式详解,外观模式也叫“过程模式,外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用

模式介绍

UML类图

类图解析:

外观模式案例:

背景介绍:

组建一个家庭影院:DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要求完成使用家庭影院的功能,通过直接用遥控器(统筹各设备开关)进行控制,其过程为:

DVD、Popcorn、Projector、Screen、Stereo、TheaterLight代码如下:

public class DVDPlayer {
    // 使用单例模式
    private static  final DVDPlayer instance = new DVDPlayer();
    private 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 static final Popcorn instance = new Popcorn();
    private 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 class Projector {
    private static final Projector instance = new Projector();
    private Projector(){}
    public static Projector getInstance() {
        return instance;
    }
    public void on() {
        System.out.println("投影仪打开了...");
    }
    public void off() {
        System.out.println("投影仪关闭了...");
    }
    public void focus() {
        System.out.println("投影仪聚焦中...");
    }
}
public class Screen {
    private static final Screen instance = new Screen();
    private Screen(){}
    public static Screen getInstance() {
        return instance;
    }
    public void up() {
        System.out.println("屏幕上升...");
    }
    public void down() {
        System.out.println("屏幕下降...");
    }
}
public class Stereo {
    private static final Stereo instance = new Stereo();
    private Stereo(){}
    public static Stereo getInstance() {
        return instance;
    }
    public void on() {
        System.out.println("立体音响打开...");
    }
    public void off() {
        System.out.println("立体音响关闭...");
    }
    public void up() {
        System.out.println("立体音响音量+...");
    }
    public void down() {
        System.out.println("立体音响音量-...");
    }
}
public class TheaterLight {
    private static final TheaterLight instance = new TheaterLight();
    private TheaterLight(){}
    public static TheaterLight getInstance() {
        return instance;
    }

    public void on() {
        System.out.println("灯光打开...");
    }
    public void off() {
        System.out.println("灯光关闭...");
    }
    public void dim() {
        System.out.println("灯光亮度调暗...");
    }
    public void bright() {
        System.out.println("灯光亮度调亮...");
    }
}

HomeTheaterFacade(统筹各设备开关)

public class HomeTheaterFacade {
    private DVDPlayer dvdPlayer;
    private Popcorn popcorn;
    private Projector projector;
    private Stereo stereo;
    private Screen screen;
    private TheaterLight theaterLight;
    public HomeTheaterFacade() {
        this.dvdPlayer = DVDPlayer.getInstance();
        this.popcorn = Popcorn.getInstance();
        this.projector = Projector.getInstance();
        this.stereo = Stereo.getInstance();
        this.screen = Screen.getInstance();
        this.theaterLight = TheaterLight.getInstance();
    }

    /**
     * 准备开始
     */
    public void ready() {
        popcorn.on();
        popcorn.pop();
        screen.down();
        projector.on();
        projector.focus();
        stereo.on();
        dvdPlayer.on();
        theaterLight.dim();
    }

    /**
     * 播放
     */
    public void play() {
        dvdPlayer.play();
    }

    /**
     * 暂停
     */
    public void pause() {
        dvdPlayer.pause();
    }

    /**
     * 结束
     */
    public void end() {
        popcorn.off();
        theaterLight.bright();
        screen.up();
        projector.off();
        stereo.off();
        dvdPlayer.off();
    }
}

Client(测试)

public class Client {
    public static void main(String[] args) {
        HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
        System.out.println("----------准备开始-----------");
        homeTheaterFacade.ready();
        System.out.println("----------开始播放-----------");
        homeTheaterFacade.play();
        System.out.println("----------播放暂停-----------");
        homeTheaterFacade.pause();
        System.out.println("----------播放结束-----------");
        homeTheaterFacade.end();
    }
}

实现结果:

外观模式的注意事项和细节

到此这篇关于Java设计模式中的外观模式详解的文章就介绍到这了,更多相关Java外观模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文