文章目录
一、外观模式定义
别名: 门面模式
类型: 结构型模式
目的: 添加一个新接口,封装多个接口调用,从而隐藏系统的复杂性。
二、 例子
2.1 自定义例子
2.1.1 定义服务1、2、3接口
public class Service1 {
public void run() {...}
}
public class Service2 {
public void excute() {...}
}
public class Service3 {
public void start() {...}
}
2.1.2 定义外观类 ServiceFacade
public class ServiceFacade {
private Service1 service1 = new Service1();
private Service2 service2 = new Service2();
private Service3 service3 = new Service3();
// 外观接口
public void run() {
service1.run();
service2.excute();
service3.start();
//或许有其他逻辑代码
}
}
2.2 Spring源码——ProxyFactoryBean
public class ProxyFactoryBean extends ProxyCreatorSupport implements FactoryBean<Object>, BeanClassLoaderAware, BeanFactoryAware {
@Nullable
public Object getObject() throws BeansException {
this.initializeAdvisorChain();
if (this.isSingleton()) {
return this.getSingletonInstance();
} else {
if (this.targetName == null) {
this.logger.info("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property.");
}
return this.newPrototypeInstance();
}
}
private synchronized Object getSingletonInstance() {
if (this.singletonInstance == null) {
this.targetSource = this.freshTargetSource();
if (this.autodetectInterfaces && this.getProxiedInterfaces().length == 0 && !this.isProxyTargetClass()) {
Class<?> targetClass = this.getTargetClass();
if (targetClass == null) {
throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
}
this.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
}
super.setFrozen(this.freezeProxy);
this.singletonInstance = this.getProxy(this.createAopProxy());
}
return this.singletonInstance;
}
}
2.3 日志门面框架slf4j
门面接口Logger
public interface Logger {
void debug(String var1);
}
public abstract class AbstractLogger implements Logger, Serializable {
public void debug(String msg) {
if (this.isDebugEnabled()) {
this.handle_0ArgsCall(Level.DEBUG, (Marker)null, msg, (Throwable)null);
}
}
}
三、其他设计模式
创建型模式
结构型模式
行为型模式
- 1、设计模式——访问者模式(Visitor Pattern)+ Spring相关源码
- 2、设计模式——中介者模式(Mediator Pattern)+ JDK相关源码
- 3、设计模式——策略模式(Strategy Pattern)+ Spring相关源码
- 4、设计模式——状态模式(State Pattern)
- 5、设计模式——命令模式(Command Pattern)+ Spring相关源码
- 6、设计模式——观察者模式(Observer Pattern)+ Spring相关源码
- 7、设计模式——备忘录模式(Memento Pattern)
- 8、设计模式——模板方法模式(Template Pattern)+ Spring相关源码
- 9、设计模式——迭代器模式(Iterator Pattern)+ Spring相关源码
- 10、设计模式——责任链模式(Chain of Responsibility Pattern)+ Spring相关源码
- 11、设计模式——解释器模式(Interpreter Pattern)+ Spring相关源码