简易计算器之工厂设计模式(最终章)

Operation抽象类

/**
 * 运算的抽象类
 */
public abstract class Operation {
  public double numFir;
  public double numSec;

  public abstract double getResult();

  // 单参数
  public Operation(double numFir) {
    super();
    this.numFir = numFir;
  }

  // 双参数
  public Operation(double numFir, double numSec) {
    super();
    this.numFir = numFir;
    this.numSec = numSec;
  }


  public double getNumFir() {
    return numFir;
  }

  public void setNumFir(double numFir) {
    this.numFir = numFir;
  }

  public double getNumSec() {
    return numSec;
  }

  public void setNumSec(double numSec) {
    this.numSec = numSec;
  }

}

OperationFactory工厂类

/**
 * 操作工厂类
 */
public class OperationFactory {


  public static Operation createOperation(double numFir, double numSec, char operator) {

    Operation oper = null;

    switch (operator) {
      case '+': {
        oper = new Add(numFir, numSec);
        break;
      }
      case '-': {
        oper = new Subtract(numFir, numSec);
        break;
      }
      case '*': {
        oper = new Multiply(numFir, numSec);
        break;
      }
      case '/': {
        oper = new Divide(numFir, numSec);
        break;
      }
      default:
        try {
          throw new Exception("No Such Method");
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
    }
    return oper;
  }
}

具体Operation运算类

加法

/**
 * 加法
 */
public class Add extends Operation {

  public Add(double numFir, double numSec) {
    super(numFir, numSec);
  }

  @Override
  public double getResult() {
    return numFir + numSec;
  }

}

减法

/**
 * 減法
 */
public class Subtract extends Operation {

  public Subtract(double numFir, double numSec) {
    super(numFir, numSec);
    // TODO Auto-generated constructor stub
  }

  @Override
  public double getResult() {
    // TODO Auto-generated method stub
    return numFir-numSec;
  }

}

乘法

/**
 * 乘法
 */
public class Multiply extends Operation{

  public Multiply(double numFir, double numSec) {
    super(numFir, numSec);
    // TODO Auto-generated constructor stub
  }

  @Override
  public double getResult() {
    // TODO Auto-generated method stub
    return numFir*numSec;
  }

}

除法

/**
 * 除法
 */
public class Divide extends Operation{

  public Divide(double numFir, double numSec) {
    super(numFir, numSec);
    // TODO Auto-generated constructor stub
  }

  @Override
  public double getResult() {
    // TODO Auto-generated method stub
    double result = 0;
    try {
      result=numFir/numSec;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      try {
        throw new Exception("被除数不能为0");
      } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }
    return result;
  }

}

测试

public class Test {

  public static void main(String[] args) {

    System.out.println(OperationFactory.createOperation(1.3D, 4.0D, '+').getResult());
  }

}

计算器代码地址:
https://github.com/Haskell2015/MaterialDesign/tree/master/calculator

你可能感兴趣的:(简易计算器之工厂设计模式(最终章))