前言
还记得当初写 spring-session 实现分布式集群 session 的共享的时候, 里面有说到利用 filter 和 HttpServletRequestWrapper 可以定制自己的 getSession 方法, 实现对 session 的控制, 从而将 session 存放到统一的位置进行存储, 达到 session 共享的目的. 但是具体是如何实现的没有提及, 今天我们就从源码的角度来看看 shiro 的 session 共享实现.
路漫漫其修远兮, 吾将上下而求索!
GitHub:https://GitHub.com/youzhibing
码云(gitee):https://gitee.com/youzhibing
装饰模式
进入正题之前我们先来看看另外一个内容, 放松下心情. 尽管目前房价依旧很高, 但还是阻止不了大家对新房的渴望和买房的热情. 如果大家买的是毛坯房, 无疑还有一项艰巨的任务要面对, 那就是装修. 对新房进行装修并没有改变房屋用于居住的本质, 但它可以让房子变得更漂亮, 更温馨, 更实用, 更能满足居家的需求. 在软件设计中, 我们也有一种类似新房装修的技术可以对已有对象 (新房) 的功能进行扩展(装修), 以获得更加符合用户需求的对象, 使得对象具有更加强大的功能. 这种技术对应于一种被称之为装饰模式的设计模式.
装饰者模式又名包装模式, 以对客户端透明的方式拓展对象的功能, 能够让我们在不修改底层代码的情况下, 给我们的对象赋予新的职责. 是继承关系的一个替代方案.
装饰模式类图
装饰模式中的角色:
抽象构件 (Component) 角色: 给出一个抽象接口, 以规范准备接收附加责任的对象.
具体构件 (ConcreteComponent) 角色: 定义一个将要接收附加责任的类.
装饰 (Decorator) 角色: 持有一个构件 (Component) 对象的实例, 并定义一个与抽象构件接口一致的接口.
具体装饰 (ConcreteDecorator) 角色: 负责给构件对象 "贴上" 附加的责任
源代码实现
Component.java
- public interface Component {
- void sampleOperation();
- }
- View Code
ConcreteComponent.java
- public class ConcreteComponent implements Component {
- @Override
- public void sampleOperation() {
- // 写具体业务代码
- System.out.println("我是 ConcreteComponent");
- }
- }
- View Code
Decorator.java
- public class Decorator implements Component {
- private Component component;
- public Decorator(Component component) {
- this.component = component;
- }
- @Override
- public void sampleOperation() {
- // 委派给具体的构建
- component.sampleOperation();
- }
- }
- View Code
ConcreteDecorator.java
- public class ConcreteDecorator extends Decorator{
- public ConcreteDecorator(Component component) {
- super(component);
- }
- @Override
- public void sampleOperation() {
- // 写相关的业务代码
- System.out.println("调用 component 方法之前业务处理");
- super.sampleOperation();
- // 写相关的业务代码
- System.out.println("调用 component 方法之后业务处理");
- }
- }
- View Code
更多详情在
来源: https://www.cnblogs.com/youzhibing/p/9568178.html