Skip to content

设计模式速查表 ​

创建型 ​

模式意图关键词
工厂方法让子类决定创建什么create()
抽象工厂创建一族产品createA() + createB()
单例全局唯一getInstance()
建造者分步构建复杂对象builder().a().b().build()
原型克隆对象clone()

结构型 ​

模式意图关键词
适配器接口转换Adapter implements Target
装饰器动态叠加功能Decorator wraps Component
代理控制访问Proxy implements Subject
外观统一入口Facade.method()
桥接抽象与实现分离Abstraction → Implementor
组合树形结构Component.add(child)
享元共享细粒度对象FlyweightFactory.get(key)

行为型 ​

模式意图关键词
策略算法可替换context.setStrategy(s)
观察者一对多通知subject.notify()
模板方法固定流程可变步骤abstract step()
责任链请求沿链传递handler.setNext(h)
命令请求封装为对象command.execute()
状态状态决定行为state.handle()
迭代器顺序访问元素iterator.next()
中介者集中交互mediator.notify()
备忘录保存/恢复状态memento.getState()
访问者作用于对象族visitor.visit(element)
解释器定义文法并解释语言expression.interpret()

JDK 中的模式 ​

模式JDK 实现
工厂Calendar.getInstance()
单例Runtime.getRuntime()
建造者Stream.Builder
适配器Arrays.asList()
装饰器BufferedReader
代理Proxy.newProxyInstance()
观察者Observable(已废弃)
策略Comparator
模板方法AbstractList
迭代器Iterator

Spring 中的模式 ​

模式Spring 实现
工厂BeanFactory
单例Bean 默认 Scope
代理AOP(JDK 动态代理 / CGLIB)
模板方法JdbcTemplate、RestTemplate
观察者ApplicationEvent
策略Resource 接口(资源抽象与多态替换,非经典策略模式)
责任链HandlerInterceptor
适配器HandlerAdapter

Released under the MIT License.