设计模式学习笔记二十七、外观模式代码

代码

package com.hao.demo.design.facade;

/**
 * @author haoxiansheng
 * @date 2020-05-18
 */
public class DVDPlayer {


    // 使用单例模式
    private static DVDPlayer instance = new 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 开始play");
    }

    public void  pause() {
        System.out.println("DVD pause");
    }
}

package com.hao.demo.design.facade;

/**
 * @author
 * @date 2020-05-18
 */
public class Popcorn {

    private static Popcorn instance = new Popcorn();

    public static Popcorn getInstance() {
        return instance;
    }

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

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

    public void poping() {
        System.out.println("Popcorn 开始制作爆米花");
    }

}


package com.hao.demo.design.facade;

/**
 * @author
 * @date 2020-05-18
 */
public class Projector {
    private static Projector instance = new Projector();

    public static Projector getInstance() {
        return instance;
    }

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

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

    public void focus() {
        System.out.println("Projector is focusing");
    }

}


package com.hao.demo.design.facade;

/**
 * @author
 * @date 2020-05-18
 */
public class Screen {

    private static Screen instance = new Screen();

    public static Screen getInstance() {
        return instance;
    }

    public void screeUp() {
        System.out.println("scree Up");
    }

    public void screeDowm() {
        System.out.println("scree Dowm ");
    }

}

package com.hao.demo.design.facade;

/**
 * @author
 * @date 2020-05-18
 */
public class Stereo {

    private static Stereo instance = new Stereo();

    public static Stereo getInstance() {
        return instance;
    }

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

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

    public void up() {
        System.out.println("Stereo 声音调大");
    }

    public void  down() {
        System.out.println("Stereo 声音减小");
    }
}

package com.hao.demo.design.facade;

/**
 * @author
 * @date 2020-05-18
 */
public class TheaterLight {

    private static TheaterLight instance = new TheaterLight();

    public static TheaterLight getInstance() {
        return instance;
    }

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

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

    public void dim() {
        System.out.println("TheaterLight dim --");
    }

    public void  bright() {
        System.out.println("TheaterLight bright");
    }
}


package com.hao.demo.design.facade;

/**
 * @author
 * @date 2020-05-18
 */
// 外观类
public class HomeTheaterFacade {

    // 定义属性
    private TheaterLight theaterLight;

    private Popcorn popcorn;

    private Stereo stereo;

    private Projector projector;

    private Screen screen;

    private DVDPlayer dvdPlayer;

    // 构造器
    public HomeTheaterFacade() {
        this.dvdPlayer = DVDPlayer.getInstance();
        this.popcorn = Popcorn.getInstance();
        this.projector = Projector.getInstance();
        this.screen = Screen.getInstance();
        this.stereo = Stereo.getInstance();
        this.theaterLight = TheaterLight.getInstance();
    }

    // 分成四步
    public void ready() {
        popcorn.on();
        popcorn.poping();

        screen.screeDowm();

        projector.on();

        stereo.on();

        dvdPlayer.on();

        dvdPlayer.on();

        theaterLight.dim();
    }
    public void play() {
        dvdPlayer.play();

        stereo.up();
        stereo.down();
    }

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

    public void end() {
        popcorn.off();

        theaterLight.bright();

        screen.screeUp();

        projector.off();

        stereo.off();

        dvdPlayer.off();
    }
}


package com.hao.demo.design.facade;

/**
 * @author
 * @date 2020-05-18
 */
public class Client {
    public static void main(String[] args) {
        // 方式一直接调用 麻烦不方便

        // 方法二 外观模式
        HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();

        homeTheaterFacade.ready();
        homeTheaterFacade.play();
        homeTheaterFacade.pause();
        homeTheaterFacade.end();
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值