文集地址
一句话总结外观模式
概述
- 外观模式又叫作门面模式,主要作用于封装子系统,为其提供一个统一调用接口,使其更方便使用,但却不关心子系统实现方式。
应用案例
- 吃糖,需要先把糖拿过来,让后撕开糖纸,在把糖放嘴里,这些个步骤缺一不可,其实可以将这些步骤提前做好,这样在外人看来,你的糖就在嘴里
实现案例
/**
* 具体行为
*
* @author ext.liuyan10
* @date 2021/2/7 17:11
*/
public class HaveSweets {
public void look() {
System.out.println("将糖果拿过来");
}
public void tearOpen() {
System.out.println("撕开糖纸");
}
public void eat() {
System.out.println("把糖吃进嘴里");
}
}
/**
* 外观角色
*
* @author ext.liuyan10
* @date 2021/2/7 17:13
*/
public class Appearance {
public void haveSweets() {
HaveSweets haveSweets = new HaveSweets();
haveSweets.look();
haveSweets.tearOpen();
haveSweets.eat();
}
}
/**
* @author ext.liuyan10
* @date 2021/2/7 17:13
*/
public class AppearanceApp {
public static void main(String[] args) {
Appearance appearance = new Appearance();
appearance.haveSweets();
}
}
Connected to the target VM, address: '127.0.0.1:64221', transport: 'socket'
将糖果拿过来
撕开糖纸
把糖吃进嘴里
Disconnected from the target VM, address: '127.0.0.1:64221', transport: 'socket'
Process finished with exit code 0
网友评论