java中接口的妙用(二):
1. 写在前面:
本例子是另外一篇博文“java中接口的妙用(一)”的姊妹篇。在进行程序设计时,通常会进行模块划分,从顶层到底层;而且,尽可能使各个模块进行松耦合,这样,当修单个模块时,做到不影响其它模块。下面这个例子,演示了如何进行分层设计,并且使用interface(接口)来进行数据的转移。
2. 例子业务说明:
本例子只是从技术角度出发,来说明“分层后,如何用inteface在层与层之间架起数据传递(处理)的桥梁,从而达到低耦合的目的。
本例子只有一个业务,计算两个数的桑,如果除数为0时,进行了处理(逐级上报)。本例分为三层:
用户层:就是有main函数的类;
业务层:只能被用户层调用,通过interface将结果返回给用户层;
系统层:只能被业务层调用通过interface将结果返回给业务层。
3. 全部代码如下:
1)用户层:
//文件:JavaTest.java
package com.my.test;
import com.common.func.GetFunc_Name;
import com.module.business.BusinessInterface;
import com.module.business.BusinessProcess;
public class JavaTest {
private BusinessInterfaceImp mBusinessInterfaceImp = new BusinessInterfaceImp();
int dataRes = -1;
public static void main(String args[]) {
JavaTest test = new JavaTest();
test.InterfaceTest();
}
// 测试函数
public void InterfaceTest() {
BusinessProcess bp = new BusinessProcess(mBusinessInterfaceImp);
int x = 5;
int y =2;
System.out.println("TestCase 001, input:"+ "x = " + x + ", y = " +y);
//调用业务层
bp.BusinessDevide(x, y);
System.out.println("TestCase 001, dataRes:"+ dataRes);
dataRes = -1; //为了测试,这个结果值再归-1;
int x1 = 10;
int y1 = 0;
System.out.println("\n");
System.out.println("TestCase 002, input:"+ "x1 = " + x1 + ", y1 = " +y1);
//调用业务层
bp.BusinessDevide(x1, y1);
System.out.println("TestCase 002, dataRes:"+ dataRes);
}
//BusinessInterface的实现类
public class BusinessInterfaceImp implements BusinessInterface{
public void onBusinessResult(int type, int data) {
System.out.println("onBusinessResult " + data);
dataRes = data; //这样,我们就达到了我们要的结果。
}
public void onBusinessError(int type) {
System.out.println("onBusinessError ");
}
}
public void Func() {
String str = "c.opj";
int sub = str.lastIndexOf("/");
String s = str.substring(sub+1);
System.out.println("Str:" + str);
System.out.println("s:" + s);
System.out.println(GetFunc_Name._FILE_());
System.out.println(GetFunc_Name._FUNC_());
System.out.println(GetFunc_Name._LINE_());
System.out.println(GetFunc_Name._TIME_());
}
}
2) 业务层:
//文件1:BusinessInterface.java
package com.module.business;
public interface BusinessInterface {
public void onBusinessResult(int type, int data);
public void onBusinessError(int type);
}
//文件2:BusinessProcess.java
package com.module.business;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.module.system.SystemInterface;
import com.module.system.SystemModule;
public class BusinessProcess {
public static int Business_OK = 1;
public static int Business_Error = 2;
BusinessInterface bListener;
public BusinessProcess(BusinessInterface bListener) {
this.bListener = bListener;
}
private SystemInterface SystemInterfaceImpl = new SystemInterface() {
public int onSystemProcess(int error, int res) {
if (error < 0) {
//错误,返回Business_Error
bListener.onBusinessError(error);
return Business_Error;
}
//正确,则将res继续返回上层
bListener.onBusinessResult(error, res);
return Business_OK;
}
};
//test function
public int BusinessDevide(int x, int y) {
SystemModule s = new SystemModule(SystemInterfaceImpl);
//调用系统层
return s.devide(x, y);
}
}
3). 系统层:
//文件1:SystemInterface.java
package com.module.system;
public interface SystemInterface {
public int onSystemProcess(int error, int data);
}
//文件2:SystemModule.java
package com.module.system;
public class SystemModule {
SystemInterface sListener;
public SystemModule(SystemInterface sListener) {
this.sListener = sListener;
}
//系统层的真正的计算函数
public int devide(int x, int y) {
int res = -100;
try {
res = x/y;
return sListener.onSystemProcess(1, res);
}catch(Exception e) {
e.printStackTrace();
return sListener.onSystemProcess(-1,res);
}
}
}
4. 测试结果:
TestCase 001, input:x = 5, y = 2
onBusinessResult 2
TestCase 001, dataRes:2
TestCase 002, input:x1 = 10, y1 = 0
onBusinessError
TestCase 002, dataRes:-1
java.lang.ArithmeticException: / by zero
at com.module.system.SystemModule.devide(SystemModule.java:18)
at com.module.business.BusinessProcess.BusinessDevide(BusinessProcess.java:38)
at com.my.test.JavaTest.InterfaceTest(JavaTest.java:33)
at com.my.test.JavaTest.main(JavaTest.java:15)
5. 附上: 代码目录结构:
< 完>