当前位置: 首页 > news >正文

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

代码文件目录:

 Subsystem:
Amplifier
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");}@Overridepublic String toString() {return "Amplifier{" +"volume=" + volume +'}';}
}
PopcornPopper
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!");}@Overridepublic String toString() {return this.getClass().getSimpleName();}
}

Projector

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!");}@Overridepublic String toString() {return this.getClass().getSimpleName();}
}

Screen

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!");}@Overridepublic String toString() {return this.getClass().getSimpleName();}
}
StreamPlayer
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!");}@Overridepublic String toString() {return this.getClass().getSimpleName();}
}
TheaterLights
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%");}@Overridepublic String toString() {return this.getClass().getSimpleName();}
}
HomeTheaterFacade
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
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
外观模式:为子系统中的一组接口提供统一的接口。外观定义了一个更高级别的接口,使得子系统更容易被使用当用户类要通过调用一系列的组件类的接口来实现某个最终的目的的时候,可以将这些组件类的接口统合在一起,形成一个新的接口,然后客户直接调用该接口,实现解耦合。最少知识原则:一个对象只调用这些方法:对象自身的、作为参数传给方法发对象的、该方法创建或者实例化的任何对象、对象的任何组件。比如:1. return this.car.start();  可以2. return this.car.engine.start(); 不可以优缺点:一方面减少耦合度,减低维护成本;另一方面会使得“包装者”类增加,造成复杂度和开发时间增加同时还会降低运行时的性能。

http://www.lryc.cn/news/122138.html

相关文章:

  • 前端杂项-个人总结八股文的背诵方案
  • 利用 3D 地理空间数据实现Cesium的沉浸式环境
  • 微服务——ES实现自动补全
  • 北斗+5G 织就精确定位的“天罗地网”
  • Ansible Roles详解
  • 微服务学习笔记-基本概念
  • Linux查看GPU显卡/CPU内存/硬盘信息
  • SQLAlchemy 入门:Python 中的 SQL 工具包和 ORM
  • react Hook+antd封装一个优雅的弹窗组件
  • HICP学习--BGP综合小实验
  • grafana中利用变量来添加dashboard详情页地址实现点击跳转
  • 正则表达式练习
  • leetcode做题笔记73矩阵置零
  • 【2.1】Java微服务: Nacos的使用
  • vue安装及环境配置
  • java使用正则表达式时遇到的问题
  • Git介绍及常用命令详解
  • 赛事 | 第25届中国机器人及人工智能大赛全国决赛榜单发布
  • JavaScript+Asp.Net MVC5同时下载多个文件
  • 如何实现前后端分离-----前端笔记
  • Ceph入门到精通-分布式存储产品的测试实践
  • 【java】设计模式——单例模式
  • 【编织时空一:探究顺序表与链表的数据之旅】
  • Tesseract用OpenCV进行文本检测
  • XLua案例学习
  • Linux:Shell编程之免交互
  • 最强自动化测试框架Playwright(18)- 执行js脚本
  • 阿里云云主机_ECS云服务器_轻量_GPU_虚拟主机详解
  • [QT编程系列-41]:Qt QML与Qt widget 深入比较,快速了解它们的区别和应用场合
  • springboot 使用zookeeper实现分布式锁