第四讲:工厂模式在开发中的运用
运用面向对象的思想将加法运算封装起来.
如果不希望 Operation operation = new AddOperation(); new一个Operation那就使用简单工厂模式.
以下就是使用了简单工厂模式之后直接从工厂类里面获取AddOperation.
Operation operation = OperationFactory.getOperation(oper);
operation.setNum1(num1);
operation.setNum2(num2);
result = operation.getResult();
要添加的话直接在工厂类里面添加判断和添加操作类.这样程序拥有了扩展性.你可以无限扩展.符合了开放封闭原则中的开放原则.那么现在是封闭的吗?它不是封闭的,因为现在添加操作的话工厂类也是要修改的.
那么现在使用工厂方法模式,把工厂类抽象出来,把具体的操作交给它的子类(具体工厂类).
所以工厂方法模式在客户端里面必须进行判断, 这就是简单工厂模式和工厂方法模式在计算器这个例子中的应用.
在面试中写出简单工厂模式即可,工厂方法模式并不强求.可以应用更高级的技术:反射来动态加载哪个类.
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 OperationFactory { public static Operation getOperation(String oper){ if("+".equals(oper)){ return new AddOperation(); }else if ("-".equals(oper)){ return new SubtractionOperation(); } return null; } } */ public interface OperationFactory{ //public Operation getOperation(String oper); public Operation getOperation(); }
public class AddOperationFactory implements OperationFactory{ @Override //public Operation getOperation(String oper) { public Operation getOperation() { // TODO Auto-generated method stub return new AddOperation(); } }
public class SubtractionOperation extends Operation{ @Override public double getResult() { // TODO Auto-generated method stub //return this.getNum1()-this.getNum2(); double result = this.getNum1()-this.getNum2(); return result; } }
import java.util.Scanner; public class MainClass { public static void main(String[] args) { //1.接收控制台输入 System.out.println("-----计算机程序-----------"); System.out.println("输入第一个操作数"); Scanner scanner = new Scanner(System.in); String strNum1 = scanner.nextLine(); System.out.println("输入运算符"); String oper = scanner.nextLine(); System.out.println("输入第二个操作数"); String strNum2 = scanner.nextLine(); double result = 0; double num1 = Double.parseDouble(strNum1); double num2 = Double.parseDouble(strNum2); //2.进行运算 /* if("+".equals(oper)){ //result = Double.parseDouble(strNum1) + Double.parseDouble(strNum2); Operation operation = new AddOperation(); operation.setNum1(num1); operation.setNum2(num2); result = operation.getResult(); } else if("-".equals(oper)){ result = Double.parseDouble(strNum1) - Double.parseDouble(strNum2); } else if("*".equals(oper)){ result = Double.parseDouble(strNum1) * Double.parseDouble(strNum2); } else if("/".equals(oper)){ result = Double.parseDouble(strNum1) / Double.parseDouble(strNum2); }*/ /*Operation operation = OperationFactory.getOperation(oper); operation.setNum1(num1); operation.setNum2(num2); result = operation.getResult(); */ if("+".equals(oper)){ OperationFactory factory = new AddOperationFactory(); Operation operation = factory.getOperation(); operation.setNum1(num1); operation.setNum2(num2); result = operation.getResult(); } //3.返回结果 System.out.println(strNum1 + oper + strNum2 + "=" +result); } }