Mybatis源码与Spring源码中设计模式的应用总结

1. MyBatis源码中应用的设计模式

设计模式 相关类

Builder模式

SqlSessionFactoryBuilder、XMLConfigBuilder、XMLMapperBuilder、XMLStatementBuilder、CacheBuilder
工厂模式 SqlSessionFactory、ObjectFactory、MapperProxyFactory
单例模式 ErrorContext和LogFactory
代理模式 Mybatis实现的核心,比如MapperProxy、ConnectionLogger,用的jdk的动态代理;还有executor.loader包使用了cglib或者javassist达到延迟加载的效果
组合模式 SqlNode和各个子类ChooseSqlNode等
模板方法模式 BaseExecutor和SimpleExecutor,还有BaseTypeHandler和所有的子类例如IntegerTypeHandler
适配器模式 Log的Mybatis接口和它对jdbc、log4j等各种日志框架的适配实现
装饰者模式 Cache包中的cache.decorators子包中等各个装饰者的实现
迭代器模式 PropertyTokenizer

 

2. Spring源码中应用的设计模式

设计模式 相关类
工厂模式 BeanFactory

单例模式

Spring依赖注入Bean实例默认是单例的。

Spring的依赖注入(包括lazy-init方式)都是发生在AbstractBeanFactory的getBean里,getBean的doGetBean方法调用getSingleton进行bean的创建

装饰器模式 Spring中用到的包装器模式在类名上有两种表现,类名中含有Wrapper 和 Decorator

代理模式

AOP

观察者模式

事件驱动模型-观察者模式 ,Observer模式-listener

ApplicationContextEvent、ApplicationListener

 

策略模式

 

资源访问Resource接口实现类

UrlResource、ClassPathResource、FileSystemResource、ServletContextResource、InputStreamResource、ByteArrayResource

 

 


 

 

posted @ 2020-12-30 17:21  songoo  阅读(203)  评论(0编辑  收藏  举报