《HeadFirst设计模式(第二版)》第七章代码——外观模式

代码文件目录:
Subsystem:
Amplifier
java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

//扬声器
public class Amplifier {
    int volume = 0;//音量

    public void on(){
        System.out.println("The amplifier is on!");
    }

    public void off(){
        System.out.println("The amplifier is off!");
    }

    public void setStreamingPlayer(){
        System.out.println("The amplifier setting to streamingPlayer mode!");
    }

    public void setVolume(int volume) {
        System.out.println("The amplifier volume is set to 5!");
        this.volume = volume;
    }

    public void setSurroundSound(){
        System.out.println("The amplifier is set to SurroundSound mode");
    }

    @Override
    public String toString() {
        return "Amplifier{" +
                "volume=" + volume +
                '}';
    }
}
PopcornPopper
java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

public class PopcornPopper {
    public void on(){
        System.out.println("The popcorn popper is on!");
    }

    public void off(){
        System.out.println("The popcorn popper is off!");
    }

    public void pop(){
        System.out.println("The popcorn popper is popping!");
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

Projector

java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

//投影仪
public class Projector {
    public void on(){
        System.out.println("The Projector is on!");
    }

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

    public void WideScreenMode(){
        System.out.println("The Projector is in WideScreenMode!");
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

Screen

java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

public class Screen {
    public void up(){
        System.out.println("The screen going up!");
    }

    public void down(){
        System.out.println("The screen going down!");
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}
StreamPlayer
java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

public class StreamPlayer {
    String movie;

    public void on(){
        System.out.println("The StreamPlayer is on!");
    }

    public void off(){
        System.out.println("The StreamPlayer is off!");
    }

    public void pause(){
        System.out.println("The StreamPlayer is pausing!");
    }

    public void play(String movie){
        this.movie= movie;
        System.out.println("The StreamPlayer is playing the "+this.movie);
    }

    public void stop(){
        System.out.println("The StreamPlayer stops!");
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}
TheaterLights
java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

public class TheaterLights {
    public void on(){
        System.out.println("The theater lights are on!");
    }

    public void off(){
        System.out.println("The theater lights are off!");
    }

    public void dim(){
        //这里偷懒不将灯的亮度设置为属性了
        System.out.println("The theater lights are dimming to 10%");
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}
HomeTheaterFacade
java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern;

import Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem.*;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

public class HomeTheaterFacade {
    Amplifier amp;
    StreamPlayer player;
    Projector projector;
    TheaterLights lights;
    Screen screen;
    PopcornPopper popper;


    public HomeTheaterFacade(Amplifier amp,
                             StreamPlayer player,
                             Projector projector,
                             TheaterLights lights,
                             Screen screen,
                             PopcornPopper popper) {
        this.amp = amp;
        this.player = player;
        this.projector = projector;
        this.lights = lights;
        this.screen = screen;
        this.popper = popper;
    }

    public void watchMovie(String movie){
        System.out.println("Get ready to watch a movie!");
        this.popper.on();
        this.popper.pop();

        this.lights.on();
        this.lights.dim();

        this.screen.down();

        this.projector.on();
        this.projector.WideScreenMode();

        this.amp.on();
        this.amp.setStreamingPlayer();
        this.amp.setSurroundSound();
        this.amp.setVolume(5);

        this.player.on();
        this.player.play(movie);
        System.out.println("Now, enjoy the movie!\n\n");
    }

    public void endMovie(){
        System.out.println("Shutting movie theater down!");
        this.popper.off();
        this.player.stop();
        this.player.off();
        this.amp.off();
        this.projector.off();
        this.screen.up();
        this.lights.off();
    }
}
HomeTheaterTestDrive
java 复制代码
package Chapter7_AdapterAndFacadePattern.FacadePattern;

import Chapter7_AdapterAndFacadePattern.FacadePattern.Subsystem.*;

/**
 * @Author 竹心
 * @Date 2023/8/8
 **/

public class HomeTheaterTestDrive {
    public static void main(String[] args) {
        Amplifier amplifier = new Amplifier();
        StreamPlayer streamPlayer = new StreamPlayer();
        Projector projector = new Projector();
        PopcornPopper popper = new PopcornPopper();
        TheaterLights lights = new TheaterLights();
        Screen screen = new Screen();

        HomeTheaterFacade homeTheater = new HomeTheaterFacade(amplifier,
                streamPlayer,projector,lights,screen,popper);

        homeTheater.watchMovie("Titanic");

        homeTheater.endMovie();

    }
}
notes.txt
java 复制代码
外观模式:
    为子系统中的一组接口提供统一的接口。外观定义了一个更高级别的接口,使得子系统更容易被使用

    当用户类要通过调用一系列的组件类的接口来实现某个最终的目的的时候,可以将这些组件类的接口
    统合在一起,形成一个新的接口,然后客户直接调用该接口,实现解耦合。

最少知识原则:
    一个对象只调用这些方法:对象自身的、作为参数传给方法发对象的、该方法创建或者实例化的任何对象、
    对象的任何组件。

    比如:
        1. return this.car.start();  可以
        2. return this.car.engine.start(); 不可以

    优缺点:一方面减少耦合度,减低维护成本;另一方面会使得"包装者"类增加,造成复杂度和开发时间增加
        同时还会降低运行时的性能。
相关推荐
字节全栈_PVK15 分钟前
微服务配置中心 Apollo解析——Portal 关联 Namespace
java·前端·微服务
钮钴禄·爱因斯晨9 小时前
赛博算卦之周易六十四卦JAVA实现:六幺算尽天下事,梅花化解天下苦。
java
wu_yi_min9 小时前
Spring Boot 日志:项目的“行车记录仪”
java·数据库·spring boot
小万编程9 小时前
【2025最新计算机毕业设计】基于SpringBoot+Vue爬虫技术的咖啡与茶饮料文化平台(高质量源码,可定制,提供文档,免费部署到本地)
java·vue.js·spring boot·毕业设计·课程设计·计算机毕业设计·项目源码
kongxx10 小时前
Maven运行任何命令都报错“Internal error: java.lang.ArrayIndexOutOfBoundsException”
java·开发语言·maven
怜渠客10 小时前
关于Java的HttpURLConnection重定向问题 响应码303
android·java
tingting011910 小时前
私有包上传maven私有仓库nexus-2.9.2
java·maven
天天向上杰11 小时前
简识JVM中并发垃圾回收器和多线程并行垃圾回收器的区别
java·jvm·算法
UVCuttt11 小时前
三天急速通关JavaWeb基础知识:Day 1 后端基础知识
java·servlet·java-ee·tomcat
一张假钞11 小时前
Sqoop源码修改:增加落地HDFS文件数与MapTask数量一致性检查
java·hadoop·hdfs·sqoop