1. 定义
外观模式是为了给客户端的一个一组接口提供一个统一的接口,是子系统更容易使用
2. 优点
更容易使用,客户端不需要和子系统在做更多的交互,也不需要了解子系统更多的实现细节
将客户端与子系统解耦,让子系统内部模块更容易扩展和使用
可以更好的隐藏子系统的内部细节
3. 参与者
1 facade 实现子系统的门面
2 subclass 子系统
4. 缺点
增加子系统,扩展子系统容易引入风险
不符合开闭原则
5. 类型
结构
6. coding
package com.maidou.learning.design.structure.facade;
public class FacadeA {
private IService serviceA = new ServiceAImpl();
private IService serviceB = new ServiceBImpl();
public FacadeA() {
}
public void getService() {
serviceA.method();;
serviceB.method();
}
}
package com.maidou.learning.design.structure.facade;
public interface IService {
void method();
}
package com.maidou.learning.design.structure.facade;
public class ServiceAImpl implements IService {
@Override
public void method() {
System.out.println("a 实现");
}
}
package com.maidou.learning.design.structure.facade;
public class ServiceBImpl implements IService {
@Override
public void method() {
System.out.println("b 实现");
}
}
package com.maidou.learning.design.structure.facade;
public class FacadeA {
private IService serviceA = new ServiceAImpl();
private IService serviceB = new ServiceBImpl();
public FacadeA() {
}
public void getService() {
serviceA.method();;
serviceB.method();
}
}
7. 源码应用
- spring jdbcutils
- mybatis configuation
- tomcat requestFacade responseFacade