您的位置:首页 > 移动开发

SPRING与设计模式---装饰模式(Decorator)(包装器模式(Wrapper))

2018-02-24 16:10 651 查看
SPRING与设计模式---装饰器模式
装饰器模式 允许向一个现有的对象添加新的功能,同时又不改变其结构。装饰者可以在所委托被装饰者的行为之前或之后加上自己的行为,以达到特定的目的(如:功能的增强)。

实际应用案例: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 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: