1.用简单工厂模式实现计算器
相当于抽象product类 public abstract class Operation { private double num1; private double num2; public double getNum1() { return num1; } public void setNum1(double num1) { this.num1 = num1; } public double getNum2() { return num2; } public void setNum2(double num2) { this.num2 = num2; } public abstract double getResult(); } 具体产品类: public class AddOperation extends Operation{ @Override public double getResult() { // TODO Auto-generated method stub double result = this.getNum1() + this.getNum2(); return result; } } public class SubOperation extends Operation{ @Override public double getResult() { // TODO Auto-generated method stub double result = this.getNum1() - this.getNum2(); return result; } } //工厂类: public class OperationFactory { public static Operation getOperation(String oper){ Operation operation = null; if("+".equals(oper)){ operation = new AddOperation(); } if("-".equals(oper)){ operation = new SubOperation(); } return operation; } } 测试: public class MainClass { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入数字1:"); String num1 = scanner.nextLine(); System.out.println("请输入操作符:"); String oper = scanner.nextLine(); System.out.println("请输入数字2:"); String num2 = scanner.nextLine(); Operation operation = OperationFactory.getOperation(oper); operation.setNum1(Double.parseDouble(num1)); operation.setNum2(Double.parseDouble(num2)); double result = operation.getResult(); System.out.println(result); } }
2.用工厂方法模式实现:
相当于抽象product类 public abstract class Operation { private double num1; private double num2; public double getNum1() { return num1; } public void setNum1(double num1) { this.num1 = num1; } public double getNum2() { return num2; } public void setNum2(double num2) { this.num2 = num2; } public abstract double getResult(); } 具体产品类: public class AddOperation extends Operation{ @Override public double getResult() { // TODO Auto-generated method stub double result = this.getNum1() + this.getNum2(); return result; } } public class SubOperation extends Operation{ @Override public double getResult() { // TODO Auto-generated method stub double result = this.getNum1() - this.getNum2(); return result; } }
工厂: public interface OperationFactoryMethod { public Operation getOperation(); } 实现类(创建对象): public class AddOperationFactory implements OperationFactoryMethod{ @Override public Operation getOperation() { // TODO Auto-generated method stub return new AddOperation(); } } public class SubOperationFactory implements OperationFactoryMethod{ @Override public Operation getOperation() { // TODO Auto-generated method stub return new SubOperation(); } } 测试: public class MainClass { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入数字1:"); String num1 = scanner.nextLine(); System.out.println("请输入操作符:"); String oper = scanner.nextLine(); System.out.println("请输入数字2:"); String num2 = scanner.nextLine(); double result = 0; if("+".equals(oper)){ Operation operation = new AddOperationFactory().getOperation(); operation.setNum1(Double.parseDouble(num1)); operation.setNum2(Double.parseDouble(num2)); result = operation.getResult(); } if("-".equals(oper)){ Operation operation = new SubOperationFactory().getOperation(); operation.setNum1(Double.parseDouble(num1)); operation.setNum2(Double.parseDouble(num2)); result = operation.getResult(); } System.out.println(result); } }
运行结果: