区分 singleton pattern 与 Spring 中的 singleton bean
singleton bean 只能保证每个容器内,相同 id 的 bean 单实例
定义 Separate the construction of a complex object from its representation so that the same construction process can create different representations
它的主要亮点有三处:
较为灵活的构建产品对象
(RabbitMQ中构建交换机等都是构建者模式)
在不执行最后 build 方法前,产品对象都不可用
构建过程采用链式调用,看起来比较爽
Spring 中体现 Builder 模式的地方:
org.springframework.beans.factory.support.BeanDefinitionBuilder
org.springframework.web.util.UriComponentsBuilder
org.springframework.http.ResponseEntity.HeadersBuilder
org.springframework.http.ResponseEntity.BodyBuilder
定义 Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses
根据上面的定义,Spring 中的 ApplicationContext 与 BeanFactory 中的 getBean 都可以视为工厂方法
,它隐藏了 bean (产品)的创建过程和具体实现
Spring 中其它工厂:
org.springframework.beans.factory.FactoryBean(bean实例化的一种方式)
@Bean 标注的静态方法及实例方法
ObjectFactory 及 ObjectProvider
前两种工厂主要封装第三方的 bean 的创建过程,后两种工厂可以推迟 bean 创建,解决循环依赖及单例注入多例等问题
定义 Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces
典型的实现有两处:
定义 Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
典型实现有:
composite 对象的作用是,将分散的调用集中起来,统一调用入口
,它的特征是,与具体干活的实现实现同一个接口,当调用 composite 对象的接口方法时,其实是委托具体干活的实现来完成
定义 :动态的为一个类增加新的功能,装饰模式是一种用于代替继承技术的模式
,无需通过继承增加子类就能够灵活的扩展新功能
。
典型实现:
定义:对另外一个对象提供代理或者占位,用来控制对这个对象的访问
装饰器模式注重的是功能增强,避免子类继承方式进行功能扩展,而代理模式更注重控制目标的访问
典型实现:
定义:可以有多个请求的处理对象,串成一个链;每个对象都有机会处理这个请求处理,请求就会沿着处理链依次向后传递,直到遇到能够处理该请求的对象
典型实现:
定义:定义对象之间的一对多依赖关系,以便在一个对象更改状态时,自动通知并更新其所有依赖关系
典型实现:
定义:定义一系列算法,封装每个算法,并使它们可互换。策略允许算法独立于使用它的客户端而变化
典型实现:
定义:在操作中定义算法的框架,将一些步骤推迟到子类。模板方法允许子类在不改变算法结构的情况下重新定义算法的某些步骤
典型实现:
大部分以 Template 命名的类,如 JdbcTemplate,TransactionTemplate