Java 拦截器模式

一、拦截器接口

package com.example.study.interceptor;

/**
 * 拦截器接口:看具体操作,也可以加入泛型,灵活变化
 */
public abstract class Interceptor {
    // 在 action 之前调用
    abstract void before(ActionInvocation invocation);

    String intercept(ActionInvocation invocation) {
        before(invocation);
        String result = invocation.invoke();
        after(invocation);
        return result;
    }

    // 在 action 之后调用
    abstract void after(ActionInvocation invocation);

    // action接口,就是需要执行具体动作的地方
    public interface Action {
        String execute();
    }

    // action 调度器
    public interface ActionInvocation {
        String invoke();
    }

}

二、拦截器具体实现

package com.example.study.interceptor;

public class TestInterceptor extends Interceptor {

    @Override
    public void before(ActionInvocation invocation) {
        System.out.println("test before ---> ");
    }
    @Override
    public void after(ActionInvocation invocation) {
        System.out.println("test after ---> ");
    }
}

package com.example.study.interceptor;

public class InitInterceptor extends Interceptor {

    @Override
    public void before(ActionInvocation invocation) {
        System.out.println("init before ---> ");
    }

    @Override
    public void after(ActionInvocation invocation) {
        System.out.println("init after ---> ");
    }
}

package com.example.study.interceptor;

public class LoginInterceptor extends Interceptor {

    @Override
    public void before(ActionInvocation invocation) {
        System.out.println("login before ---> ");
    }

    @Override
    public void after(ActionInvocation invocation) {
        System.out.println("login after ---> ");
    }
}

三、调度器具体实现

package com.example.study.interceptor;

import java.util.ArrayList;
import java.util.List;

public class ActionInvoationImpl implements Interceptor.ActionInvocation {

    private int index = 0;
    private Interceptor.Action action;
    private List<Interceptor> interceptors = new ArrayList<>();

    public void setAction(Interceptor.Action action) {
        this.action = action;
    }

    public void addInterceptor(Interceptor interceptor) {
        interceptors.add(interceptor);
    }

    @Override
    public String invoke() {
        String result = "";
        if (index == interceptors.size()) {
            result = action.execute();
        } else {
            Interceptor interceptor = interceptors.get(index);
            index++;
            result = interceptor.intercept(this);
        }
        return result;
    }
}

四、具体调用

package com.example.study;

import com.example.study.interceptor.ActionInvoationImpl;
import com.example.study.interceptor.InitInterceptor;
import com.example.study.interceptor.Interceptor;
import com.example.study.interceptor.LoginInterceptor;
import com.example.study.interceptor.TestInterceptor;

public class MyClass {

    public static void main(String[] args) {

        LoginInterceptor loginInterceptor = new LoginInterceptor();
        TestInterceptor testInterceptor = new TestInterceptor();
        InitInterceptor initInterceptor = new InitInterceptor();

        ActionInvoationImpl impl = new ActionInvoationImpl();
        impl.addInterceptor(loginInterceptor);
        impl.addInterceptor(testInterceptor);
        impl.addInterceptor(initInterceptor);

        Interceptor.Action action = new Interceptor.Action() {
            @Override
            public String execute() {
                System.out.println("action execute ...");
                return "hello world";
            }
        };
        impl.setAction(action);

        String result = impl.invoke();
        System.out.println("Action result ---> " + result);
    }

}

五、结果查看

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值