攔截過濾器模式
阿新 • • 發佈:2018-12-12
過濾器
攔截器的目的是想在做一些事前之前,處理一些動作,主要是思想,具體實現都是無所謂的
設計思想
- 攔截器可以採用連結串列或者陣列實現,這裡採用List
- doSomething這裡採用靜態代理,返回一個攔截器代理類,執行doSomething
簡單的程式碼實現
原操作類
public class Person {
public void doSomeThing(String msg) {
System.out.println("doSomeThing ,然後輸出資訊: " + msg);
}
}
攔截器
public interface Intercepter { /** * @param target 原操作類 * @param args 方法引數 */ void intercep(Object target, Object... args); } public class AuthIntercepter implements Intercepter { @Override public void intercep(Object target, Object... args) { if (target instanceof Person) { System.out.println("你有許可權操作 doSomething方法"); } else { throw new RuntimeException("非法操作"); } } } public class ArgsIsNullIntercepter implements Intercepter { @Override public void intercep(Object target, Object... args) { if (target instanceof Person) { Objects.requireNonNull(args); Objects.requireNonNull(args[0]); //doSomething有個msg引數 System.out.println("引數校驗通過"); } } } public class OtherIntercepter implements Intercepter { @Override public void intercep(Object target, Object... args) { if (target instanceof Person) { System.out.println("攔截器要做一些處理"); } } } public class OtherIntercepter implements Intercepter { @Override public void intercep(Object target, Object... args) { if (target instanceof Person) { System.out.println("攔截器要做一些處理"); } } }
攔截器代理類
public class PersonIntercepterProxy { // 攔截器佇列 private List<Intercepter> intercepters = new ArrayList<Intercepter>(16); // 靜態代理 private Person person; public PersonIntercepterProxy(Person person) { this.person = person; } public void registerIntercepter(Intercepter intercepter) { if (intercepter != null) { intercepters.add(intercepter); } } public void unRegisterIntercepter(Intercepter intercepter) { if (intercepter != null) { intercepters.remove(intercepter); } } public void doSomething(String msg) { // 先執行攔截器佇列 for (Intercepter intercepter : intercepters) { intercepter.intercep(person, msg); } // 再執行原操作類 person.doSomeThing(msg); } }
測試
public class IntercepterTest { public static void main(String[] args) { // 原操作類 Person person = new Person(); // 攔截器代理類 PersonIntercepterProxy proxy = new PersonIntercepterProxy(person); // 註冊攔截器 proxy.registerIntercepter(new AuthIntercepter()); proxy.registerIntercepter(new ArgsIsNullIntercepter()); proxy.registerIntercepter(new OtherIntercepter()); // doSomething proxy.doSomething(" hello 你好"); } }
測試結果
你有許可權操作 doSomething方法
引數校驗通過
攔截器要做一些處理
doSomeThing ,然後輸出資訊: hello 你好
更改測試類
public class IntercepterTest {
public static void main(String[] args) {
// 原操作類
Person person = new Person();
// 攔截器代理類
PersonIntercepterProxy proxy = new PersonIntercepterProxy(person);
// 註冊攔截器
proxy.registerIntercepter(new AuthIntercepter());
proxy.registerIntercepter(new ArgsIsNullIntercepter());
proxy.registerIntercepter(new OtherIntercepter());
// doSomething
proxy.doSomething(null);
}
}
測試結果
你有許可權操作 doSomething方法
Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Unknown Source)
at demo.impl.ArgsIsNullIntercepter.intercep(ArgsIsNullIntercepter.java:14)
at demo.PersonIntercepterProxy.doSomething(PersonIntercepterProxy.java:33)
at demo.IntercepterTest.main(IntercepterTest.java:19)