一、外观模式
提供一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。
二、举例
请设计一个类,要求如下
- 电影类
- 投影仪类
- 将看电影的整个复杂过程包起来
电影类
package com.jackeys.AppearanceMode;
/**
* @Description: 电影类
* @ClassName: Movie
* @Package com.jackeys.AppearanceMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/19 16:59
* @Version: 1.0
*/
public class Movie {
/**
* 电影名
*/
private String name;
public Movie(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
投影仪类
package com.jackeys.AppearanceMode;
/**
* @Description: 投影仪
* @ClassName: Projector
* @Package com.jackeys.AppearanceMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/19 17:00
* @Version: 1.0
*/
public class Projector {
public void setMovie(Movie movie) {
System.out.println("放入电影:" + movie.getName());
}
public void outMovie(Movie movie) {
System.out.println("拿出电影:" + movie.getName());
}
public void onProjector() {
System.out.println("打开投影仪");
}
public void offProjector() {
System.out.println("关闭投影仪");
}
public void onMovie() {
System.out.println("播放电影");
}
public void offMovie() {
System.out.println("关闭电影");
}
}
外观类
package com.jackeys.AppearanceMode;
/**
* @Description: 外观模式
* @ClassName: Facade
* @Package com.jackeys.AppearanceMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/19 17:04
* @Version: 1.0
*/
public class Facade {
/**
* 电影
*/
private Movie movie;
/**
* 投影仪
*/
private Projector projector;
public Facade(Movie movie, Projector projector) {
this.movie = movie;
this.projector = projector;
}
/**
* @MethodName: watchMovie
* @Description: 播放电影
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/19 17:06
* @Version: 1.0
*/
public void watchMovie() {
this.projector.setMovie(this.movie);
this.projector.onProjector();
this.projector.onMovie();
}
/**
* @MethodName: endMovie
* @Description: 结束电影
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/19 17:07
* @Version: 1.0
*/
public void endMovie() {
this.projector.offMovie();
this.projector.outMovie(this.movie);
this.projector.offProjector();
}
}
测试类
package com.jackeys.AppearanceMode;
/**
* @Description: 测试外观模式
* @ClassName: Test
* @Package com.jackeys.AppearanceMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/19 17:07
* @Version: 1.0
*/
public class Test {
public static void main(String[] args) {
Facade facade = new Facade(new Movie("长津湖"), new Projector());
facade.watchMovie();
facade.endMovie();
}
}
运行截图