簡單的處理鏈-java

我們都知道設計模式有一個責任鏈模式,責任鏈模式是知道前後要執行的步驟,而處理鏈是不知道的,下面看一下代碼:

首先創建一個處理鏈的對象類

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

/**
 * 處理鏈的對象類
 * @auther QiaoZhenwu
 * @date 2017年1月12日 下午3:33:07
 */
public class ServiceHandler {

	private String serviceType;//服務類型
	
	private List<String> serviceList = new ArrayList<String>();//服務鏈
	
	private String serviceListener;
	
	public ServiceHandler(String serviceType, String serviceListener){
		this.serviceType = serviceType;
		this.serviceListener = serviceListener;
	}
	
	public ServiceHandler addHandler(String handler) {
		serviceList.add(handler);
		return this;
	}

	public String getServiceType() {
		return serviceType;
	}

	public void setServiceType(String serviceType) {
		this.serviceType = serviceType;
	}

	public List<String> getServiceList() {
		return serviceList;
	}

	public void setServiceList(List<String> serviceList) {
		this.serviceList = serviceList;
	}

	public String getServiceListener() {
		return serviceListener;
	}

	public void setServiceListener(String serviceListener) {
		this.serviceListener = serviceListener;
	}
}

然後,寫一個接口,讓需要實現功能的類去實現這個接口

/**
 * @auther QiaoZhenwu
 * @date 2017年1月12日 下午3:54:05
 */
public interface Test {

	public void handle(User u)  throws Exception ;
}

幾個接口的實現類,分別實現自己不同的功能

package listhandle;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @auther QiaoZhenwu
 * @date 2017年1月12日 下午3:47:06
 */
public class Test1 implements Test {

	public void handle(User u) throws Exception {
		System.out.print("我是Test1******");
		u.setID("37028218154514551");
		Utils.doMeth(u);
		System.out.println();
	}
}
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @auther QiaoZhenwu
 * @date 2017年1月12日 下午3:47:12
 */
public class Test2 implements Test {

	public void handle(User u)  throws Exception {
		System.out.print("我是Test2******");
		u.setHigher("1.78");
		Utils.doMeth(u);
		System.out.println();
	}
}
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @auther QiaoZhenwu
 * @date 2017年1月12日 下午3:47:22
 */
public class Test3 implements Test {

	public void handle(User u)  throws Exception {
		System.out.print("我是Test3******");
		u.setWeight("60KG");
		Utils.doMeth(u);
		System.out.println();
	}
}

一個User的對象類

/**
 * User對象類
 * @auther QiaoZhenwu
 * @date 2017年1月12日 下午4:26:41
 */
public class User {
	
	private String ID;
	
	private String higher;
	
	private String weight;

	private String name;
	
	private Integer age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getID() {
		return ID;
	}

	public void setID(String iD) {
		ID = iD;
	}

	public String getHigher() {
		return higher;
	}

	public void setHigher(String higher) {
		this.higher = higher;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}
}

最後一個測試類

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 處理鏈的測試類
 * @auther QiaoZhenwu
 * @date 2017年1月12日 下午3:47:52
 */
public class ServiceChain {

	public static final String ROUTE = "listhandle";// 包路徑

	public static final String meName = "handle";// 接口方法名

	public List<ServiceHandler> adapters() {
		List<ServiceHandler> lis = new ArrayList<>();
		lis.add(new ServiceHandler("purchase", "listener")
				.addHandler(Test1.class.getSimpleName())
				.addHandler(Test2.class.getSimpleName())
				.addHandler(Test3.class.getSimpleName()));
		lis.add(new ServiceHandler("cancel", "listener")
				.addHandler(Test1.class.getSimpleName())
				.addHandler(Test3.class.getSimpleName())
				.addHandler(Test2.class.getSimpleName()));
		return lis;
	}

	public void getHandler(String serviceType) {
		List<ServiceHandler> lis = adapters();//設置兩種執行順序
		User u = new User();
		u.setName("qzw");
		u.setAge(26);
		lis.forEach(l -> {
			if (l.getServiceType().equals(serviceType)) {
				l.getServiceList().forEach(sl -> {
						 Utils.doMethod(sl, u);
					});
			}
		});
	}

	public static void main(String[] args) {
		new ServiceChain().getHandler("purchase");
		System.out.println("================================================================================");
		new ServiceChain().getHandler("cancel");
	}

}

幫助工具類

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @auther QiaoZhenwu
 * @date 2017年4月21日 下午3:46:12
 */
public class Utils {
	
	public static final String ROUTE = "listhandle";// 包路徑
	
	public static final String meName = "handle";// 接口方法名

	public static void doMethod(String clsName, Object u){
		try {
			Object obj = Class.forName(ROUTE + "." + clsName).newInstance();
			Class cls = obj.getClass();
			Method me = cls.getDeclaredMethod(meName, User.class);
			Class<?>[] pt = me.getParameterTypes();
			if (!me.getName().startsWith("set") && !me.getName().startsWith("get")) {
				 for(Class c: pt){
					Method method = cls.getMethod(meName, c);
					method.invoke(obj, u);
				 }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void doMeth(Object u){
		Class clazz = u.getClass();
		Field[] fields = u.getClass().getDeclaredFields();// 獲得屬性
		for (Field field : fields) {
			PropertyDescriptor pd;
			try {
				pd = new PropertyDescriptor(field.getName(), clazz);
				Method getMethod = pd.getReadMethod();// 獲得get方法
				Object o = getMethod.invoke(u);// 執行get方法返回一個Object
				System.out.print(field.getName() + ":" + o + "--");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

這樣就實現了一個簡單的處理連,

在測試類的adapters根據需求寫多個不同的處理鏈,可以按照需要設定不同的執行順序


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章