简单的处理链-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根据需求写多个不同的处理链,可以按照需要设定不同的执行顺序


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