最近用了策略模式+抽象工厂+反射写了一个计算器的Demo,本来写好了加法和减法,通过设计模式实现了不修改代码,通过增加新类和修改配置文件就可以增加乘法和除法,符合开闭原则。
分析
什么是反射
要想明白什么是反射,首先你要明白什么是正射,直接把类实例化就是正射
比如:
Person person=new Person();//这就是正射
反射则是运行是才知道创建的是哪个类,并且可以获取这个类的属性和方法
//获取配置文件的类名 count count=new count(); in = count.getClass().getResourceAsStream("/application.properties"); //通过工厂创建类 Strategy strategy= (Strategy) Class.forName("com."+countType).newInstance();
这样就实现了运行时才知道创建了哪些类,当你输入称号时,系统会自动匹配乘法需要创建哪个类,然后去工厂创建它,就可以使用了
原代码
package com; import com.Context; import com.count; public class whichCaculate { public static int switchMethods(int num1, String Num ,int num2){ count cou=new count(); int result=0;// 计算结果 switch(Num){ case "+": Context context=new Context(new add()); result=context.executeStrategy(num1,num2); break; case "-": context=new Context(new sub()); result=context.executeStrategy(num1,num2); break; } return result; } }
可以看出,原来是由计算类来控制是加法还是减法,一旦增加乘法,就需要去修改计算类,这样就违背了开闭原则。
代码
加法类 add.java
package com; public class add implements Strategy { @Override public int doOperation(int num1, int num2) { return num1 + num2; } }
减法类 add.java
package com; public class sub implements Strategy { @Override public int doOperation(int num1,int num2){ return num1-num2; } }
抽象工厂 countFactory.java
package com; public class countFactory { public Strategy getcount(String countType) throws ClassNotFoundException, IllegalAccessException, InstantiationException { Strategy strategy= (Strategy) Class.forName("com."+countType).newInstance(); return strategy; } }
Context类
package com; public class Context { private Strategy strategy; public Context(Strategy strategy){ this.strategy=strategy; } public int executeStrategy(int num1,int num2){ return strategy.doOperation(num1,num2); } }
Strategy 接口
package com; public interface Strategy { public int doOperation(int num1, int num2); }
主函数
package com; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Scanner; public class count { private static int firstNum; private static String operate; private static int secondNum; public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException { Scanner sc=new Scanner(System.in);//实例化提取类 System.out.println("请输入数字"); firstNum=Integer.parseInt(sc.nextLine());//接收键盘输入 System.out.println("请输入运算符"); operate=sc.nextLine(); System.out.println("请输入数字"); secondNum=Integer.parseInt(sc.nextLine()); count count=new count(); InputStream in = null; Properties props = new Properties(); //第一种方法,取得src下的属性文件,成功 in = count.getClass().getResourceAsStream("/application.properties"); props.load(in); //输出属性文件中的信息 String key =operate; String value=props.getProperty(key); countFactory countfa=new countFactory(); Context context =new Context(countfa.getcount(value)); int sum=context.executeStrategy(firstNum,secondNum); System.out.println(sum); } } 配置文件 ```java +=add -=sub
增加乘法
首先创建乘法类multiplication.java
package com; public class multiplication implements Strategy { @Override public int doOperation(int num1, int num2) { return num1 * num2; } }
然后增加配置文件
+=add -=sub *=multiplication
然后就可以计算乘法了