以下是运算小框架的制作过程,以及核心代码,完成(计算,监听,回馈等):
package com.demo2.operator;
/**
* 运算标准接口
* @author Liudeli
*/
public interface IOperator {
/**
* 设置第一个数
* @param number1
* @return
*/
public Operator setNumber1(double number1);
/**
* 设置运算符
* @param operator
* @return
*/
public Operator setOperator(String operator);
/**
* 设置第二个数
* @param number2
* @return
*/
public Operator setNumber2(double number2);
/**
* 设置运算结果
* @param onResult
* @return
*/
public Operator setOnResultListener(OnResult onResult);
/**
* 设置运算出错
* @param onError
* @return
*/
public Operator setOnErrorListener(OnError onError);
/**
* 次方法为最后提交的方法(必须最后提交调用)
*/
public void operatorCommit();
}
package com.demo2.operator;
/**
* 运算配置类
* @author Liudeli
*/
public class OperatorConfig {
/**
* 定义返回错误码(运算符输入错误)
*/
public static final int ERROR_CODE_INPUT = 4004;
/**
* 定义返回错误码(运算发生异常)
*/
public static final int ERROR_CODE_EX = 4005;
/**
* 运算默认值
*/
public static final double RESULT_DEFAUTL = 0.0;
}
package com.demo2.operator;
/**
* 运算结果接口
* @author Liudeli
*/
public interface OnResult {
/**
* 获取得到运算结果
* @param result
*/
public void result(double result);
}
package com.demo2.operator;
/**
* 次接口为运算出错接口
* @author Liudeli
*
*/
public interface OnError {
/**
* 错误码方法
* @param error
*/
public void error(int error);
}
package com.demo2.operator;
/**
* 此类为运算类的具体实现
* @author Liudeli
*/
public class Operator implements IOperator {
/**
* 定义单例模式
*/
private static Operator mOperator = null;
public static Operator getInstance() {
if (null == mOperator) {
synchronized (Operator.class) {
mOperator = new Operator();
}
}
return mOperator;
}
/**
* 此方法的用于计算两个数的值
* @param figure1 第一个数值
* @param figure2 第二个数值
* @param operator 传入运算符
* @return
*/
private double getOperatorResult(double figure1,
double figure2, String operator) {
double resultOperator = OperatorConfig.RESULT_DEFAUTL;
try {
if ("+".equals(operator)) {
resultOperator = figure1 + figure2;
} else if ("-".equals(operator)) {
resultOperator = figure1 - figure2;
} else if ("*".equals(operator)) {
resultOperator = figure1 * figure2;
} else if ("/".equals(operator)) {
resultOperator = figure1 / figure2;
} else {
onError.error(OperatorConfig.ERROR_CODE_INPUT);
}
} catch (Exception e) {
e.printStackTrace();
onError.error(OperatorConfig.ERROR_CODE_EX);
}
return resultOperator;
}
private double number1;
public Operator setNumber1(double number1) {
this.number1 = number1;
return this;
}
private String operator;
public Operator setOperator(String operator) {
this.operator = operator;
return this;
}
private double number2;
public Operator setNumber2(double number2) {
this.number2 = number2;
return this;
}
private OnResult onResult;
public Operator setOnResultListener(OnResult onResult) {
this.onResult = onResult;
return this;
}
private OnError onError;
public Operator setOnErrorListener(OnError onError) {
this.onError = onError;
return this;
}
public void operatorCommit() {
double operatorResult = getOperatorResult(number1, number2, operator);
if (OperatorConfig.RESULT_DEFAUTL != operatorResult) {
onResult.result(operatorResult);
}
}
}
使用方式,非常灵活,可以设置监听提交结合一体:
模拟客户端程序使用:
方式一:
package com.demo2.client;
import java.util.Scanner;
import com.demo2.operator.OnError;
import com.demo2.operator.OnResult;
import com.demo2.operator.Operator;
/**
* 模拟客户端程序
* @author Liudeli
*/
public class Client {
public static void main(String [] srgs) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数");
double number1 = Double.parseDouble(scanner.nextLine());
System.out.println("请输入运算符 ( + - * /)");
String operator = scanner.nextLine();
System.out.println("请输入第二数");
double number2 = Double.parseDouble(scanner.nextLine());
// 第一种用法
Operator.getInstance()
.setNumber1(number1)
.setNumber2(number2)
.setOperator(operator)
.setOnErrorListener(new OnError() {
public void error(int error) {
System.out.println("运算过程出错,错误码是:" + error);
}
})
.setOnResultListener(new OnResult() {
public void result(double result) {
System.out.println("运算结果是:" + result);
}
})
.operatorCommit();
}
}
方式二:
package com.demo2.client;
import java.util.Scanner;
import com.demo2.operator.OnError;
import com.demo2.operator.OnResult;
import com.demo2.operator.Operator;
/**
* 模拟客户端程序
* @author Liudeli
*/
public class Client2 {
public static void main(String [] srgs) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数");
double number1 = Double.parseDouble(scanner.nextLine());
System.out.println("请输入运算符 (+ - * /)");
String operator = scanner.nextLine();
System.out.println("请输入第二数");
double number2 = Double.parseDouble(scanner.nextLine());
// 第二种用法
Operator.getInstance()
.setNumber1(number1)
.setNumber2(number2)
.setOperator(operator)
.setOnErrorListener(onError)
.setOnResultListener(onResult)
.operatorCommit();
}
private static OnError onError = new OnError() {
public void error(int error) {
System.out.println("运算过程出错,错误码是:" + error);
}
};
private static OnResult onResult = new OnResult() {
public void result(double result) {
System.out.println("运算结果是:" + result);
}
};
}
方式三:
package com.demo2.client;
import java.util.Scanner;
import com.demo2.operator.OnError;
import com.demo2.operator.OnResult;
import com.demo2.operator.Operator;
/**
* 模拟客户端程序
* @author Liudeli
*/
public class Client3 {
public static void main(String [] srgs) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数");
double number1 = Double.parseDouble(scanner.nextLine());
System.out.println("请输入运算符 (+ - * /)");
String operator = scanner.nextLine();
System.out.println("请输入第二数");
double number2 = Double.parseDouble(scanner.nextLine());
// 第三种用法
Operator instance = Operator.getInstance();
instance.setNumber1(number1);
instance.setNumber2(number2);
instance.setOperator(operator);
instance.setOnErrorListener(new OnError() {
public void error(int error) {
System.out.println("运算过程出错,错误码是:" + error);
}
});
instance.setOnResultListener(new OnResult() {
public void result(double result) {
System.out.println("运算结果是:" + result);
}
});
instance.operatorCommit();
}
}
运行结果:
Project结构:
谢谢大家的观看,更多精彩技术博客,会不断的更新,请大家访问,
刘德利CSDN博客, http://blog.csdn.net/u011967006