适配器模式

1、定义
将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

2、Spring实例

Advice(通知)的类型有:BeforeAdvice、AfterReturningAdvice、ThreowSadvice的。

在每个类型Advice(通知)都有对应的拦截器:MethodBeforeAdviceInterceptor、AfterReturningAdviceInterceptor、ThrowsAdviceInterceptor。

Spring需要将每个Advice(通知)都封装成对应的拦截器类型,返回给容器,所以需要使用适配器模式对Advice进行转换。

Advice -->Adapter-->Intecepter

具体的代码:

Advice接口

public interface MethodBeforeAdvice extends BeforeAdvice {   

    void before(Method method, Object[] args, Object target) throws Throwable;

}  

Adapter接口

public interface AdvisorAdapter {  

    boolean supportsAdvice(Advice advice);  

    MethodInterceptor getInterceptor(Advisor advisor);  

} 

具体Adapter类

class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {  

    public boolean supportsAdvice(Advice advice) {  
        return (advice instanceof MethodBeforeAdvice);  
    }  

    public MethodInterceptor getInterceptor(Advisor advisor) {  
        MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();  
        return new MethodBeforeAdviceInterceptor(advice);  
    }  

} 

DefaultAdvisorAdapterRegistry类:用Adapter将Advice转换成Intecepter

public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Serializable {  

    private final List<AdvisorAdapter> adapters = new ArrayList<AdvisorAdapter>(3);  


    /** 
     * Create a new DefaultAdvisorAdapterRegistry, registering well-known adapters. 
     */  
    public DefaultAdvisorAdapterRegistry() {//这里注册了适配器  
        registerAdvisorAdapter(new MethodBeforeAdviceAdapter());  
        registerAdvisorAdapter(new AfterReturningAdviceAdapter());  
        registerAdvisorAdapter(new ThrowsAdviceAdapter());  
    }  


    public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {  
        if (adviceObject instanceof Advisor) {  
            return (Advisor) adviceObject;  
        }  
        if (!(adviceObject instanceof Advice)) {  
            throw new UnknownAdviceTypeException(adviceObject);  
        }  
        Advice advice = (Advice) adviceObject;  
        if (advice instanceof MethodInterceptor) {  
            // So well-known it doesn't even need an adapter.  
            return new DefaultPointcutAdvisor(advice);  
        }  
        for (AdvisorAdapter adapter : this.adapters) {  
            // Check that it is supported.  
            if (adapter.supportsAdvice(advice)) {//这里调用了适配器的方法  
                return new DefaultPointcutAdvisor(advice);  
            }  
        }  
        throw new UnknownAdviceTypeException(advice);  
    }  

    public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {  
        List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);  
        Advice advice = advisor.getAdvice();  
        if (advice instanceof MethodInterceptor) {  
            interceptors.add((MethodInterceptor) advice);  
        }  
        for (AdvisorAdapter adapter : this.adapters) {  
            if (adapter.supportsAdvice(advice)) {//这里调用了适配器的方法  
                interceptors.add(adapter.getInterceptor(advisor));  
            }  
        }  
        if (interceptors.isEmpty()) {  
            throw new UnknownAdviceTypeException(advisor.getAdvice());  
        }  
        return interceptors.toArray(new MethodInterceptor[interceptors.size()]);  
    }  

    public void registerAdvisorAdapter(AdvisorAdapter adapter) {  
        this.adapters.add(adapter);  
    }  

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值