- 定义:门面模式,提供了一个统一的接口,用来访问子系统中的一群接口
- 适用场景:
子系统越来越复杂,增加外观模式提供简单调用接口
构建多层系统结构,利用外观对象作为每层的入口,简化层间的调用
- 优点:
减少系统依赖,松散耦合
更好的划分访问层次
符合迪米特法则,即最小知道原则
- 缺点
不符合开闭原则
示例代码
public class GetUpService {
public void getUp(){
System.out.println("起床了");
}
}
public class EatService {
public void eat(){
System.out.println("吃饭了");
}
}
public class SchoolService {
public void toSchool(){
System.out.println("上学");
}
}
public class MorningService {
private EatService eatService=new EatService();
private GetUpService getUpService=new GetUpService();
private SchoolService schoolService=new SchoolService();
public void morningAction(){
getUpService.getUp();
eatService.eat();
schoolService.toSchool();
}
}
Test类
public class Test {
public static void main(String[] args) {
MorningService morningService=new MorningService();
morningService.morningAction();
}
}
输出
起床了
吃饭了
上学