SPRING与设计模式---装饰模式(Decorator)(包装器模式(Wrapper))
2018-02-24 16:10
651 查看
SPRING与设计模式---装饰器模式
装饰器模式 允许向一个现有的对象添加新的功能,同时又不改变其结构。装饰者可以在所委托被装饰者的行为之前或之后加上自己的行为,以达到特定的目的(如:功能的增强)。
实际应用案例:springsession框架使用HTTP请求包装类SessionRepositoryRequestWrapper和Session存储过滤器 SessionRepositoryFilter 实现实现分布式session;
UML类图:
![](http://img.blog.csdn.net/2018022721483791)
另外SessionRepositoryRequestWrapper还使用HttpSessionStrategy(使用了策略模式)
HttpSessionWrapper实现了原生的HttpSession的接口,在tomcat容器进行session.invalidate()操作时会调用SessionRepository接口进行delete操作。
其他参考文章:装饰器(Decorator)模式 http://blog.csdn.net/Xiao_Spring/article/details/68065616
装饰器模式(Decorator Pattern) http://blog.csdn.net/wwh578867817/article/details/51480441
装饰器模式 允许向一个现有的对象添加新的功能,同时又不改变其结构。装饰者可以在所委托被装饰者的行为之前或之后加上自己的行为,以达到特定的目的(如:功能的增强)。
实际应用案例:springsession框架使用HTTP请求包装类SessionRepositoryRequestWrapper和Session存储过滤器 SessionRepositoryFilter 实现实现分布式session;
UML类图:
SessionRepositoryRequestWrapper使用包装模式对原生的request的功能进行增强,可以将session中的数据和分布式数据库进行同步,这样即使当前tomcat崩溃,session中的数据也不会丢失。
用户请求通过代理服务分发到其他tomcat后,request会根据用户的cookie或者head头部信息重新从分布式数据库中将session信息还原。
另外SessionRepositoryRequestWrapper还使用HttpSessionStrategy(使用了策略模式)
实现getRequestedSessionId()方法,可以从cookie或者head头部获取sessionId.
SessionRepositoryRequestWrapper使用SessionRepository接口来进行session数据的存储,这也是策略模式。
SessionRepository接口负责Session的CRUD操作:
SessionRepositoryRequestWrapper.getSession时调用SessionRepository接口来获取Session。
SessionRepositoryResponseWrapper在onCommit时会调用SessionRepository接口进行save操作。HttpSessionWrapper实现了原生的HttpSession的接口,在tomcat容器进行session.invalidate()操作时会调用SessionRepository接口进行delete操作。
其他参考文章:装饰器(Decorator)模式 http://blog.csdn.net/Xiao_Spring/article/details/68065616
装饰器模式(Decorator Pattern) http://blog.csdn.net/wwh578867817/article/details/51480441
https://www.cnblogs.com/nick-huang/p/6986824.html
相关文章推荐
- 设计模式之装饰模式篇(Decorator)
- C#设计模式之 :Decorator模式(装饰模式)
- 简单Java代码实例助你通俗易懂的理解什么是装饰(者)设计模式 (Decorator)
- java23种设计模式--装饰模式(decorator)
- "围观"设计模式(13)--结构型之装饰模式(Decorator Pattern)
- 设计模式 - Decorator模式(装饰模式)
- C++设计模式之十四--Decorator装饰模式
- 设计模式(十二)-装饰器模式(Decorator Pattern)——装饰边框与被饰物的一致性
- Java经典设计模式-结构型模式-装饰模式(Decorator)
- JAVA设计模式之装饰模式(decorator)
- 设计模式学习-Decorator(装饰)
- 设计模式学习笔记十五:装饰模式(Decorator Pattern)
- 设计模式(3)-结构型-装饰模式(Decorator)
- 步步为营 .NET 设计模式学习笔记 十四、Decorator(装饰模式)
- 设计模式(九)之Decorator - 装饰模式
- JAVA设计模式(11):结构型-装饰模式(Decorator)
- 设计模式拾荒之装饰器模式( Decorator Pattern): Oracle 数据库打印PreparedStatement
- 设计模式-装饰模式(Decorator Pattern)
- 设计模式23---设计模式之装饰模式(Decorator)(结构型)
- 设计模式之八------Decorator(装饰)