Reflection pattern optimization of factory class with a large number of switch branches
Continue with the case of factory pattern. In the previous article, there were only two algorithm classes (addition and subtraction). Now add another multiplication
Step one:
//Arithmetic class public class Operation { private double _numberA=0; private double _numberB=0; public double get_numberA() { return _numberA; } public void set_numberA(double _numberA) { this._numberA = _numberA; } public double get_numberB() { return _numberB; } public void set_numberB(double _numberB) { this._numberB = _numberB; } /** *Get the operation result * @author ys-developer * @return * @since JDK 1.6 */ public double getResult(){ double result=0; return result; } }
The second step:
//Additive class public class OperationAdd extends Operation{ @Override public double getResult(){ double result=0; result=get_numberA()+get_numberB(); return result; } }
//Subtraction class public class OperationSub extends Operation { @Override public double getResult(){ double result=0.0; result=get_numberA()-get_numberB(); return result; } }
//multiplicative
public class OperationRide extends Operation {
@Override
public double getResult(){
double result=0.0;
result=get_numberA()*get_numberB();
return result;
}
}
The third step:
Create a properties configuration file to change the "add subtract multiply" class into a simple configuration
JIA=OperationAdd JIAN=OperationSub CHENG=OperationRide
The fourth step:
Modify factory class
//Reflection factory public class ReflexFactor { public static Operaction choose(String shortName){ Operation operation=null; //from properties Read in file shortName Corresponding full package name Properties pro=new Properties(); try { pro.load(new FileInputStream("src/Calculator.properties")); String fullName=pro.getProperty(shortName); //Equivalent to replacing the factory mode, which will directly require new Just write in the class of operation=(Operation) Class.forName(fullName).newInstance();//Note: if the corresponding class cannot be found, the previous path Class.forName("src."+fullName).newInstance(); } catch (Exception e) { e.printStackTrace(); } return operation; } }
Final test:
public class Test { public static void main(String[] args) { Operation oper=new Operation(); Scanner scan = new Scanner(System.in); boolean isok=true; while (isok) { System.out.println("please enter a number A: "); double a=scan.nextInt(); System.out.println("Please input operation symbol(+,-,*,/): "); String b=scan.next(); System.out.println("please enter a number B"); double c=scan.nextInt(); oper=ReflexFactor.choose(b); oper.set_numberA(a); oper.set_numberB(c); System.out.println("The result is equal to:"+oper.getResult()); System.out.println("Continue or not( y/n)"); String d=scan.next(); if (d.equals("n")) { break; } } } }
Summary: the modified factory class greatly reduces the switch judgment. In the future, you need to add a division. You only need to create a division class and add a configuration in the configuration file. You do not need to modify the engineering class