亲宝软件园·资讯

展开

Java外观模式

爱学习的大鱼​​​​​​​ 人气:0

模式介绍

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();
    }
}

实现结果:

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

加载全部内容

相关教程
猜你喜欢
用户评论