Java设计模式之外观模式

外观模式

外观模式,旨在将复杂的行为封装起来,只对外暴露简单的接口,无需让调用者知道复杂的实现细节。举个例子,就像我们的手机一样,外表看似简单,其实内部有着复杂的模块和线路,但是用户不需要知道其内部构造,他只需要关注手机外表和外部的按钮即可。

外观模式是一种结构型模式。

使用场景

希望隐藏内部复杂的实现细节,只对外保留简单的使用接口。

实例说明

比如现在有一个家庭音影系统,含有DVD播放器、投影仪、幕布、音响、灯光系统。

/**
 * DVD播放器
 */
public class DVDPlayer {

    public void on() {
        System.out.println("打开DVD");
    }

    public void off() {
        System.out.println("关闭DVD");
    }

    public void setDVD() {
        System.out.println("选择光盘");
    }

    public void play() {
        System.out.println("开始播放");
    }

    public void pause() {
        System.out.println("暂停播放");
    }

}
/**
 * 投影仪
 */
public class Projector {

    public void on(){
        System.out.println("打开投影仪");
    }

    public void off(){
        System.out.println("关闭投影仪");
    }

    public void focus(){
        System.out.println("对焦");
    }

    public void zoom(){
        System.out.println("调整画面大小");
    }
}

/**
 * 幕布
 */
public class Screen {

    public void uprise(){
        System.out.println("升起幕布");
    }

    public void lower(){
        System.out.println("降下幕布");
    }
}
/**
 * 音响
 */
public class Stereo {

    public void on() {
        System.out.println("打开音响");
    }

    public void off() {
        System.out.println("关闭音响");
    }

    public void setVolum() {
        System.out.println("调整音量");
    }
}
/**
 * 灯光系统
 */
public class LightingSystem {

    public void on() {
        System.out.println("打开灯光");
    }

    public void off() {
        System.out.println("关闭灯光");
    }

    public void bright() {
        System.out.println("调亮灯光");
    }

    public void dim() {
        System.out.println("调暗灯光");
    }
}

不用外观模式时,我播放dvd要做下面这些准备

public class Demo {

    public static void main(String args[]) {
        DVDPlayer player = new DVDPlayer();
        Projector projector = new Projector();
        Screen screen = new Screen();
        Stereo stereo = new Stereo();
        LightingSystem lightingSystem = new LightingSystem();

        //播放dvd
        player.on();//打开dvd
        player.setDVD();//插入光盘
        screen.lower();//降下幕布
        projector.on();//打开投影仪
        projector.focus();//对焦
        projector.zoom();//调整大小
        stereo.on();//打开音响
        lightingSystem.on();//打开灯光系统
        lightingSystem.dim();//调暗灯光
        player.play();//播放
    }
}

这个操作对与使用的人来说太复杂了,每次想要播放的时候都要进行一遍,所以我们应该对外只暴露简单的接口,把复杂的细节封装起来。

创建一个外观类,为调用端提供一个统一的调用接口。

/**
 * 外观类,对外统一调用接口
 */
public class DVDOutward {
    private DVDPlayer player;
    private Projector projector;
    private Screen screen;
    private Stereo stereo;
    private LightingSystem lightingSystem;

    public DVDOutward() {
        player = new DVDPlayer();
        projector = new Projector();
        screen = new Screen();
        stereo = new Stereo();
        lightingSystem = new LightingSystem();
    }

    public void ready(){
        player.on();//打开dvd
        player.setDVD();//插入光盘
        screen.lower();//降下幕布
        projector.on();//打开投影仪
        projector.focus();//对焦
        projector.zoom();//调整大小
        stereo.on();//打开音响
        lightingSystem.on();//打开灯光系统
        lightingSystem.dim();//调暗灯光
    }

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

    public void pause(){
        player.pause();
    }

    public void close(){
        player.off();//关闭dvd
        screen.uprise();//升起幕布
        projector.off();//关闭投影仪
        stereo.off();//关闭音响
        lightingSystem.bright();//调亮灯光
    }
}

现在,想要播放dvd只需要调用outward的ready和play方法。

public class Demo {

    public static void main(String args[]) {
       
       DVDOutward outward = new DVDOutward();
       outward.ready();
       outward.play();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值