工厂模式应用

1. 第一个计算器程序

  1. public void calculate() {
  2. System.out.println("---第一个计算器程序---");
  3. // 1、接收控制台输入
  4. Scanner scanner = new Scanner(System.in);
  5. System.out.println("输入第一个操作数");
  6. String numStr1 = scanner.nextLine();
  7. System.out.println("输入运算符");
  8. String operator = scanner.nextLine();
  9. System.out.println("输入第二个操作数");
  10. String numStr2 = scanner.nextLine();
  11. // 2、进行计算
  12. double result = 0;
  13. if ("+".equals(operator)) {
  14. result = Double.parseDouble(numStr1) + Double.parseDouble(numStr2);
  15. } else if ("-".equals(operator)) {
  16. result = Double.parseDouble(numStr1) - Double.parseDouble(numStr2);
  17. } else if ("*".equals(operator)) {
  18. result = Double.parseDouble(numStr1) * Double.parseDouble(numStr2);
  19. } else if ("/".equals(operator)) {
  20. result = Double.parseDouble(numStr1) / Double.parseDouble(numStr2);
  21. } else {
  22. result = 0;
  23. }
  24. // 3、返回结果
  25. System.out.println(numStr1 + operator + numStr2 + "=" + result);
  26. }

2. 抽象运算行为

  1. public abstract class Operation {
  2. private double num1;
  3. private double num2;
  4. public double getNum1() {
  5. return num1;
  6. }
  7. public void setNum1(double num1) {
  8. this.num1 = num1;
  9. }
  10. public double getNum2() {
  11. return num2;
  12. }
  13. public void setNum2(double num2) {
  14. this.num2 = num2;
  15. }
  16. public abstract double getResult();
  17. }
  1. public class AddOperation extends Operation {
  2. @Override
  3. public double getResult() {
  4. return this.getNum1() + this.getNum2();
  5. }
  6. }
  1. public class SubOperation extends Operation {
  2. @Override
  3. public double getResult() {
  4. return this.getNum1() - this.getNum2();
  5. }
  6. }
  1. public class MultiOperation extends Operation {
  2. @Override
  3. public double getResult() {
  4. return this.getNum1() * this.getNum2();
  5. }
  6. }
  1. public class DivOperation extends Operation {
  2. @Override
  3. public double getResult() {
  4. return this.getNum1() / this.getNum2();
  5. }
  6. }

3. 简单工厂模式

  1. public class OperationFactory {
  2. public static Operation getOperation(String operator) {
  3. if ("+".equals(operator)) {
  4. return new AddOperation();
  5. } else if ("-".equals(operator)) {
  6. return new SubOperation();
  7. } else if ("*".equals(operator)) {
  8. return new MultiOperation();
  9. } else if ("/".equals(operator)) {
  10. return new DivOperation();
  11. } else {
  12. return null;
  13. }
  14. }
  15. }
  1. // 2、进行计算
  2. double result = 0;
  3. double num1 = Double.parseDouble(numStr1);
  4. double num2 = Double.parseDouble(numStr2);
  5. Operation operation = OperationFactory.getOperation(operator);
  6. assert operation != null;
  7. operation.setNum1(num1);
  8. operation.setNum2(num2);
  9. result = operation.getResult();

4. 工厂方法模式

  1. public interface OperationFactory {
  2. Operation getOperation();
  3. }
  1. public class AddOperationFactory implements OperationFactory {
  2. @Override
  3. public Operation getOperation() {
  4. return new AddOperation();
  5. }
  6. }
  1. public class SubOperationFactory implements OperationFactory {
  2. @Override
  3. public Operation getOperation() {
  4. return new SubOperation();
  5. }
  6. }
  1. public class MultiOperationFactory implements OperationFactory {
  2. @Override
  3. public Operation getOperation() {
  4. return new MultiOperation();
  5. }
  6. }
  1. public class DivOperationFactory implements OperationFactory {
  2. @Override
  3. public Operation getOperation() {
  4. return new DivOperation();
  5. }
  6. }
  1. // 2、进行计算
  2. double result = 0;
  3. double num1 = Double.parseDouble(numStr1);
  4. double num2 = Double.parseDouble(numStr2);
  5. OperationFactory factory = null;
  6. if ("+".equals(operator)) {
  7. factory = new AddOperationFactory();
  8. } else if ("-".equals(operator)) {
  9. factory = new SubOperationFactory();
  10. } else if ("*".equals(operator)) {
  11. factory = new MultiOperationFactory();
  12. } else if ("/".equals(operator)) {
  13. factory = new DivOperationFactory();
  14. }
  15. assert factory != null;
  16. Operation operation = factory.getOperation();
  17. operation.setNum1(num1);
  18. operation.setNum2(num2);
  19. result = operation.getResult();