执行链java_简单的处理链-java

本文通过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 serviceList = new ArrayList();//服务链

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 getServiceList() {

return serviceList;

}

public void setServiceList(List 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 adapters() {

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值