1.責任鏈模式
責任鏈模式其實就是多層攔截器 定義介面
public interface interceptor1 {
public boolean before(Object proxy, Object target, Method method,Object [] args);
public void around(Object proxy,Object target, Method method, Object [] args);
public void after(Object proxy, Object target, Method method, Object [] args);
}
複製程式碼
實現介面1
package com.test3;
import java.lang.reflect.Method;
/**
* Created by leewihong on 2018/6/19.
*/
public class myinterceptor1 implements interceptor1 {
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
System.out.println("進入了攔截器1");
return true;
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
System.out.println("攔截器1around方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
System.out.println("攔截器1after方法");
}
}
複製程式碼
實現介面2
package com.test3;
import java.lang.reflect.Method;
/**
* Created by leewihong on 2018/6/19.
*/
public class myinterceptor2 implements interceptor1 {
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
System.out.println("進入攔截器2");
return true;
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
System.out.println("攔截器2around方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
System.out.println("攔截器2after方法");
}
}
複製程式碼
實現介面3
package com.test3;
import java.lang.reflect.Method;
/**
* Created by leewihong on 2018/6/19.
*/
public class myinterceptor3 implements interceptor1 {
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
System.out.println("進去攔截器3方法");
return true;
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
System.out.println("攔截器3around方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
System.out.println("攔截器3after方法");
}
}
複製程式碼
建立代理
package com.test3;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Created by leewihong on 2018/6/19.
*/
public class interceptorJDKProxy implements InvocationHandler {
public Object target = null;
public String interceptorString = null;
public interceptorJDKProxy(Object target, String interceptorString){
this.target = target;
this.interceptorString = interceptorString;
}
public static Object bind(Object target, String interceptorString){
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass()
.getInterfaces(),new interceptorJDKProxy(target,interceptorString));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (interceptorString == null){
// 反射原有的方法
return method.invoke(target,args);
}
Object result = null;
interceptor1 interceptor1 =(interceptor1) Class.forName(interceptorString).newInstance();
if (interceptor1.before(proxy,target,method,args)){
result = method.invoke(target,args);
}
else {
interceptor1.around(proxy,target,method,args);
}
interceptor1.after(proxy,target,method,args);
return result;
}
}
複製程式碼
定義測試介面
package com.test3;
/**
* Created by leewihong on 2018/6/19.
*/
public interface test3_helloword {
public void sayHelloWorld();
}
複製程式碼
實現測試介面
package com.test3;
/**
* Created by leewihong on 2018/6/19.
*/
public class test3_helloworldImpl implements test3_helloword {
@Override
public void sayHelloWorld() {
System.out.println("test3HelloWorld");
}
}
複製程式碼
測試多層攔截器
package com.test3;
/**
* Created by leewihong on 2018/6/19.
*/
public class test3 {
public static void main(String [] args){
test3_helloword test3_helloword1 = (test3_helloword) interceptorJDKProxy.bind(new
test3_helloworldImpl(),
"com.test3.myinterceptor1");
test3_helloword test3_helloword2 = (test3_helloword) interceptorJDKProxy.bind(test3_helloword1,"com" +
".test3.myinterceptor2");
test3_helloword test3_helloword3 = (test3_helloword) interceptorJDKProxy.bind(test3_helloword2,"com" +
".test3.myinterceptor3");
test3_helloword3.sayHelloWorld();
}
}
複製程式碼
測試結果
進去攔截器3方法
進入攔截器2
進入了攔截器1
test3HelloWorld
攔截器1after方法
攔截器2after方法
攔截器3after方法
複製程式碼
2.觀察者模式
定義一個被觀察者
package com.test4;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
/**
* Created by leewihong on 2018/6/20.
*/
public class ProductList extends Observable {
// 產品列表
private List<String> productlist = null;
// 類唯一例項
private static ProductList instance;
private ProductList(){
}
public static ProductList getInstance(){
if (instance == null){
instance = new ProductList();
instance.productlist = new ArrayList<String>();
}
return instance;
}
// 增加觀察者
public void addProductListObserver(Observer observer){
this.addObserver(observer);
}
// 新增產品
public void addProduct(String newProduct){
productlist.add(newProduct);
System.out.println("產品列表新增了產品"+newProduct);
this.setChanged();
this.notifyObservers(newProduct);
}
}
複製程式碼
定義一個京東觀察者
package com.test4;
import java.util.Observable;
import java.util.Observer;
/**
* Created by leewihong on 2018/6/20.
*/
public class JingDongObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
String newProduct = (String) arg;
System.out.println("傳送新產品"+newProduct+"同步到京東商城");
}
}
複製程式碼
定義一個淘寶觀察者
package com.test4;
import java.util.Observable;
import java.util.Observer;
/**
* Created by leewihong on 2018/6/20.
*/
public class TaoBaoObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
String newProduct = (String) arg;
System.out.println("傳送新產品"+newProduct+"同步到淘寶商城");
}
}
複製程式碼
測試結果
package com.test4;
/**
* Created by leewihong on 2018/6/20.
*/
public class test4 {
public static void main(String [] args){
ProductList observable = ProductList.getInstance();
TaoBaoObserver taoBaoObserver = new TaoBaoObserver();
JingDongObserver jingDongObserver = new JingDongObserver();
observable.addObserver(taoBaoObserver);
observable.addObserver(jingDongObserver);
observable.addProduct("iphoneX");
}
}
複製程式碼
輸出結果
產品列表新增了產品iphoneX
傳送新產品iphoneX同步到京東商城
傳送新產品iphoneX同步到淘寶商城
複製程式碼