天天看點

JavaOperator小架構制作【精品部落格】

以下是運算小架構的制作過程,以及核心代碼,完成(計算,監聽,回饋等):

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 = ;

    /**
     * 定義傳回錯誤碼(運算發生異常)
     */
    public static final int ERROR_CODE_EX = ;

    /**
     * 運算預設值
     */
    public static final double RESULT_DEFAUTL = ;

}
           
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);
        }
    }

}
           

使用方式,非常靈活,可以設定監聽送出結合一體:

JavaOperator小架構制作【精品部落格】

模拟用戶端程式使用:

方式一:

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();
    }
}
           

運作結果:

JavaOperator小架構制作【精品部落格】
JavaOperator小架構制作【精品部落格】

Project結構:

JavaOperator小架構制作【精品部落格】

謝謝大家的觀看,更多精彩技術部落格,會不斷的更新,請大家通路,

劉德利CSDN部落格, http://blog.csdn.net/u011967006