java 多个接口统一处理_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 多个接口统一处理

java 多个接口统一处理

 2015/1/23 1:22:06  lionzm  程序员俱乐部  我要评论(0)
  • 摘要:背景:在开发的过程中已经需要提供多个接口给外围系统。服务端在实现想使用统一的方法处理多个接口,例如判断传入的参数是否合理。1.定义两个接口packageorg.common.single.inf;importorg.common.single.condition.Condition1;publicinterfaceIQueryServiceextendsIQueryBaseService{publicvoidmyService(Condition1condition);}packageorg
  • 标签:Java 接口
背景:在开发的过程中已经需要提供多个接口给外围系统。服务端在实现想使用统一的方法处理多个接口,例如判断传入的参数是否合理
1.定义两个接口
class="java">
package org.common.single.inf;

import org.common.single.condition.Condition1;

public interface IQueryService extends IQueryBaseService{
	
	public void myService(Condition1 condition);
}



package org.common.single.inf;

import org.common.single.condition.Condition2;

public interface IQueryService2 extends IQueryBaseService{
	
	public void myService2(Condition2 condition);
}


package org.common.single.inf;

public interface IQueryBaseService {

}


这两个接口的实现类
import org.common.single.condition.Condition1;
import org.common.single.inf.IQueryBaseService;
import org.common.single.inf.IQueryService;

public class QueryServiceImpl extends QueryServiceBase implements IQueryService {
         //具体的实现类
	IQueryBaseService service;

	public QueryServiceImpl(IQueryBaseService service) {
		this.service = service;
	}

	@Override
	public void myService(Condition1 condition) {
		baseService(service,"myService",condition);
	}

}


import org.common.single.condition.Condition2;
import org.common.single.inf.IQueryBaseService;
import org.common.single.inf.IQueryService2;

public class QueryService2Impl extends QueryServiceBase implements IQueryService2 {
        //具体的实现类
	IQueryBaseService service;

	public QueryService2Impl(IQueryBaseService service) {
		this.service = service;
	}

	@Override
	public void myService2(Condition2 condition) {
		baseService(service,"myService",condition);
	}

}


import org.common.single.condition.Condition;
import org.common.single.inf.IQueryBaseService;

public class QueryServiceBase {

	protected void baseService(IQueryBaseService service, String methedName, Condition condition) {
		if (!condition.isVaild()){
			System.out.println("condition is invaild");
		} else {
			try {
				//这里的反射会存在性能问题,可使用MethodAccess缓存需要查找的函数反射的耗时
				Method m = service.getClass().getMethod(methedName, condition.getClass());
				m.invoke(service, condition);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}
}


真正的实现类
import org.common.single.condition.Condition1;
import org.common.single.inf.IQueryService;

public class QueryClient implements IQueryService{

	@Override
	public void myService(Condition1 condition) {
		System.out.println("query client service");
	}
	
}


import org.common.single.condition.Condition2;
import org.common.single.inf.IQueryService2;

public class QueryClient2 implements IQueryService2{

	@Override
	public void myService2(Condition2 condition) {
		System.out.println("query client service2");
	}
	
}


条件类:
public class Condition1 implements Condition {

	private int param;

	@Override
	public boolean isVaild() {
		return param > 0;
	}

	public int getParam() {
		return param;
	}

	public void setParam(int param) {
		this.param = param;
	}
	
}


public class Condition2 implements Condition{

	private String param;
	@Override
	public boolean isVaild() {
		return param != null;
	}

}

public interface Condition {
	public boolean isVaild();
}


测试一下:
import org.common.single.client.QueryClient;
import org.common.single.client.QueryClient2;
import org.common.single.condition.Condition1;
import org.common.single.condition.Condition2;
import org.common.single.inf.IQueryService;
import org.common.single.inf.IQueryService2;
import org.common.single.inf.impl.QueryService2Impl;
import org.common.single.inf.impl.QueryServiceImpl;

public class Main {
	public static void main(String[] args) {
		IQueryService serice = new QueryServiceImpl(new QueryClient());
		Condition1 condition = new Condition1(); 
		condition.setParam(2);
		serice.myService(condition);
		
		IQueryService2 serice2 = new QueryService2Impl(new QueryClient2());
		Condition2 condition2 = new Condition2(); 
		serice2.myService2(condition2);
	}
}


打印结果:
query client service
condition is invaild

小结:这也可以理解为父类利用反射调用子类的方法
发表评论
用户名: 匿名