Python 官方文档:入门教程 => 点击学习
目录模式介绍UML类图外观模式案例:外观模式的注意事项和细节模式介绍 外观模式(Facade) ,也叫“过程模式:外观模式为子系统中的一组接口提供一个一致的界面,此模式定
类图解析:
背景介绍:
组建一个家庭影院:DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要求完成使用家庭影院的功能,通过直接用遥控器(统筹各设备开关)进行控制,其过程为:
DVD、Popcorn、Projector、Screen、Stereo、TheaterLight代码如下:
public class DVDPlayer {
// 使用单例模式
private static final DVDPlayer instance = new DVDPlayer();
private 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 播放中...");
}
public void pause() {
System.out.println("dvd 暂停了...");
}
}
public class Popcorn {
private static final Popcorn instance = new Popcorn();
private Popcorn(){}
public static Popcorn getInstance() {
return instance;
}
public void on() {
System.out.println("爆米花机打开了...");
}
public void off() {
System.out.println("爆米花机关闭了...");
}
public void pop() {
System.out.println("爆米花做好了...");
}
}
public class Projector {
private static final Projector instance = new Projector();
private Projector(){}
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println("投影仪打开了...");
}
public void off() {
System.out.println("投影仪关闭了...");
}
public void focus() {
System.out.println("投影仪聚焦中...");
}
}
public class Screen {
private static final Screen instance = new Screen();
private Screen(){}
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println("屏幕上升...");
}
public void down() {
System.out.println("屏幕下降...");
}
}
public class Stereo {
private static final Stereo instance = new Stereo();
private Stereo(){}
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println("立体音响打开...");
}
public void off() {
System.out.println("立体音响关闭...");
}
public void up() {
System.out.println("立体音响音量+...");
}
public void down() {
System.out.println("立体音响音量-...");
}
}
public class TheaterLight {
private static final TheaterLight instance = new TheaterLight();
private TheaterLight(){}
public static TheaterLight getInstance() {
return instance;
}
public void on() {
System.out.println("灯光打开...");
}
public void off() {
System.out.println("灯光关闭...");
}
public void dim() {
System.out.println("灯光亮度调暗...");
}
public void bright() {
System.out.println("灯光亮度调亮...");
}
}
HomeTheaterFacade(统筹各设备开关)
public class HomeTheaterFacade {
private DVDPlayer dvdPlayer;
private Popcorn popcorn;
private Projector projector;
private Stereo stereo;
private Screen screen;
private TheaterLight theaterLight;
public HomeTheaterFacade() {
this.dvdPlayer = DVDPlayer.getInstance();
this.popcorn = Popcorn.getInstance();
this.projector = Projector.getInstance();
this.stereo = Stereo.getInstance();
this.screen = Screen.getInstance();
this.theaterLight = TheaterLight.getInstance();
}
public void ready() {
popcorn.on();
popcorn.pop();
screen.down();
projector.on();
projector.focus();
stereo.on();
dvdPlayer.on();
theaterLight.dim();
}
public void play() {
dvdPlayer.play();
}
public void pause() {
dvdPlayer.pause();
}
public void end() {
popcorn.off();
theaterLight.bright();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}
}
Client(测试)
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
System.out.println("----------准备开始-----------");
homeTheaterFacade.ready();
System.out.println("----------开始播放-----------");
homeTheaterFacade.play();
System.out.println("----------播放暂停-----------");
homeTheaterFacade.pause();
System.out.println("----------播放结束-----------");
homeTheaterFacade.end();
}
}
实现结果:
到此这篇关于Java设计模式中的外观模式详解的文章就介绍到这了,更多相关Java外观模式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java设计模式中的外观模式详解
本文链接: https://www.lsjlt.com/news/164606.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0