代码实战

package com.atguigu.facade;


public class DVDPlayer {

    // 使用单例模式,恶汉式
    private static DVDPlayer instance = new DVDPlayer();

    public static DVDPlayer getInstance() {
        return instance;
    }
    //开
    public void on() {
        System.out.println("DVD ON");
    }
    // 关
    public void off() {
        System.out.println("DVD off");
    }
    // 播放
    public void play() {
        System.out.println("DVD play");
    }
    // 暂停
    public void pause() {
        System.out.println("DVD pause");
    }


}
package com.atguigu.facade;


// 爆米花
public 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 play() {
        System.out.println("PopCorn play");
    }
    // 暂停
    public void pause() {
        System.out.println("PopCorn pause");
    }


}
package com.atguigu.facade;


// 投影仪
public 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 play() {
        System.out.println("Projector play");
    }
    // 暂停
    public void pause() {
        System.out.println("Projector pause");
    }
    // 暂停
    public void focus() {
        System.out.println("聚焦 pause");
    }


}
package com.atguigu.facade;

// 屏幕
public class Screen {
    private static Screen instance = new Screen();

    public static Screen getInstance() {
        return instance;
    }

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

    public void down() {
        System.out.println("Screen down");
    }
}
package com.atguigu.facade;


public class Stereo {
    private static Stereo instance = new Stereo();

    public static Stereo getInstance() {
        return instance;
    }

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

    // 关
    public void off() {
        System.out.println("Stereo off");
    }
    // 音量调大
    public void up() {
        System.out.println("Stereo up");
    }
}
package com.atguigu.facade;

public class TheaterLigth {
    private static TheaterLigth instance = new TheaterLigth();

    public static TheaterLigth getInstance() {
        return instance;
    }

    // 开
    public void on() {
        System.out.println("TheaterLigth on");
    }

    // 关
    public void dim() {
        System.out.println("TheaterLigth dim");
    }

    // 音量调大
    public void bright() {
        System.out.println("TheaterLigth bright");
    }
}

关键的部分你来了

package com.atguigu.facade;


public class HomeTheaterFacade {
    // 定义各个子系统对象
    private TheaterLigth theaterLigth;
    private PopCorn popCorn;
    private Stereo stereo;
    private Projector projector;
    private Screen screen;
    private DVDPlayer dvdPlayer;

    // 构造器
    public HomeTheaterFacade() {
        super();
        this.theaterLigth = TheaterLigth.getInstance();
        this.popCorn = PopCorn.getInstance();
        this.stereo = Stereo.getInstance();
        this.projector = Projector.getInstance();
        this.screen = Screen.getInstance();
        this.dvdPlayer = DVDPlayer.getInstance();
    }

    // 操作分成4 步
    public void ready() {
        // 打开爆米花机
        popCorn.on();
        // 工作
        popCorn.play();
        // 屏幕下来
        screen.down();
        // 投影仪打开
        projector.on();
        // 立体声打开
        stereo.on();
        // DVD打开
        dvdPlayer.on();
        // 灯光调暗
        theaterLigth.dim();
    }

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

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

    // 结束
    public void end() {
        // 打开爆米花机
        popCorn.off();
        // 灯光调亮
        theaterLigth.bright();
        // 屏幕上去
        screen.up();
        // 投影仪打开
        projector.off();
        // 立体声打开
        stereo.off();
        // DVD打开
        dvdPlayer.off();
    }

}
package com.atguigu.facade;


public class Client {
    public static void main(String[] args) {
         // 直接调用就很麻烦,嗯
        //        不知道要调用谁
        // 我封装好这个Home后,我就直接调用嗯
        // 我整个影院就能够轻松控制
        HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
        homeTheaterFacade.ready();
        homeTheaterFacade.play();
        homeTheaterFacade.pause();
        homeTheaterFacade.end();
        /*
        PopCorn ON
        PopCorn play
        Screen down
        Projector ON
        Stereo on
        DVD ON
        TheaterLigth dim
        DVD play
        DVD pause
        PopCorn off
        TheaterLigth bright
        Screen up
        Projector off
        Stereo off
        DVD off

        Process finished with exit code 0
        * */
    }
}


results matching ""

    No results matching ""