外观模式

定义一个高层接口,为子系统中的一组接口提供一个一致的外观,从而简化了该子系统的使用。

package com.strife.pattern.facade;

/**
 * 外观模式
 *
 * @author mengzhenghao
 * @date 2022/5/30
 */
public class Facade {
    public static void main(String[] args) {
        HomeFacade homeFacade = new HomeFacade();
        homeFacade.watchMovie();
        System.out.println("----------------------------------");
        homeFacade.endMovie();
    }
}

class DVDPlayer {

    private static DVDPlayer instance = new DVDPlayer();

    public static DVDPlayer getInstance() {
        return instance;
    }

    public void on() {
        System.out.println("DVDPlayer on");
    }

    public void off() {
        System.out.println("DVDPlayer off");
    }

    public void play() {
        System.out.println("DVDPlayer is playing");
    }

    public void pause() {
        System.out.println("DVDPlayer is paused");
    }
}

class Popcorn {

    private static Popcorn instance = new Popcorn();

    public static Popcorn getInstance() {
        return instance;
    }

    public void on() {
        System.out.println("Popcorn on");
    }

    public void off() {
        System.out.println("Popcorn off");
    }

    public void pop() {
        System.out.println("Popcorn is popping");
    }

}

class Projector {

    private static Projector instance = new Projector();

    public static Projector getInstance() {
        return instance;
    }

    public void on() {
        System.out.println("Projector on");
    }

    public void off() {
        System.out.println("Projector off");
    }

    public void tvMode() {
        System.out.println("Projector is in tv mode");
    }

    public void wideScreenMode() {
        System.out.println("Projector is in wide screen mode");
    }
}

class Screen {

    private static Screen instance = new Screen();

    public static Screen getInstance() {
        return instance;
    }

    public void up() {
        System.out.println("Screen is going up");
    }

    public void down() {
        System.out.println("Screen is going down");
    }
}

class HomeFacade {

    private DVDPlayer dvdPlayer;
    private Popcorn popcorn;
    private Projector projector;
    private Screen screen;

    public HomeFacade() {
        this.dvdPlayer = DVDPlayer.getInstance();
        this.popcorn = Popcorn.getInstance();
        this.projector = Projector.getInstance();
        this.screen = Screen.getInstance();
    }

    public void watchMovie() {
        popcorn.on();
        popcorn.pop();
        dvdPlayer.on();
        dvdPlayer.play();
        screen.down();
        projector.on();
        projector.tvMode();
    }

    public void endMovie() {
        popcorn.off();
        dvdPlayer.off();
        screen.up();
        projector.off();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、模式定义 外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子...
    端木轩阅读 9,151评论 2 2
  • 一、模式定义 外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子...
    QuantRuu阅读 4,117评论 0 50
  • 模式定义 外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统...
    lijun_m阅读 3,021评论 0 0
  • 文摘一:有些地方外观模式也被叫做门面模式,英文即Facade Pattern,提前说明一下。 试想这种情况,用户添...
    _浅墨_阅读 3,471评论 0 1
  • 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得...
    夜航星osmo阅读 1,603评论 0 0

友情链接更多精彩内容