- 场景:制作一个计算器的程序
- 过程式的开发
- 代码
package staticFactoryMethod; import com.sun.xml.internal.ws.util.StringUtils; import java.util.Arrays; import java.util.Scanner; import java.util.regex.Pattern; public class First { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?"); String[] number = new String[2]; do { System.out.print("请输入第一个数字:"); number[0] = scanner.nextLine(); }while (!NUMBER_PATTERN.matcher(number[0]).matches()); String[] characterArr = {"+","-","*","/"}; String character; do { System.out.print("请输入符号(+、-、*、/):"); character = scanner.nextLine(); }while (!Arrays.asList(characterArr).contains(character)); do { System.out.print("请输入第二个数字:"); number[1] = scanner.nextLine(); }while (!NUMBER_PATTERN.matcher(number[1]).matches()); Float result = null; Float[] numberF = {Float.valueOf(number[0]),Float.valueOf(number[1])}; switch(character) { case "+": result = numberF[0] + numberF[1]; break; case "-": result = numberF[0] - numberF[1]; break; case "*": result = numberF[0] * numberF[1]; break; case "/": result = numberF[0] / numberF[1]; break; } System.out.println("结果是:" + result); } } 请输入第一个数字:7 请输入符号(+、-、*、/):/ 请输入第二个数字:2 结果是:3.5
- 存在的问题
- 计算与页面没有进行分离,导致计算逻辑的代码无法得到复用(pc端和手机端需要开发两套代码)
- 没有使用面向对象的编程思想,导致了代码无法进行复用(其他部分带代码也想使用减法计算的时候需要复制粘贴,无法做到直接使用)
- 前端需要根据具体的运算方式来创建不同的对象,当增加运算类的时候,修改的地方过多。
- 代码
- 面向对象开发
- 代码
package staticFactoryMethod; import java.util.Arrays; import java.util.Scanner; import java.util.regex.Pattern; /** * 页面与逻辑分离 * 面向对象编程 */ public class Second { public static void main(String[] args) throws Exception { //前端输入 计算需要的数字和符号 Scanner scanner = new Scanner(System.in); Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?"); String[] number = new String[2]; do { System.out.print("请输入第一个数字:"); number[0] = scanner.nextLine(); }while (!NUMBER_PATTERN.matcher(number[0]).matches()); //这里是可用的计算方式 String character; do { System.out.print("请输入符号(+、-、*、/):"); character = scanner.nextLine(); }while (!OperationFactory.checkCharacter(character)); do { System.out.print("请输入第二个数字:"); number[1] = scanner.nextLine(); }while (!NUMBER_PATTERN.matcher(number[1]).matches()); OperationFactory operationFactory = new OperationFactory(); Operation operation = null; operation = operationFactory.createOperate(character); operation.setNumber1(Float.valueOf(number[0])); operation.setNumber2(Float.valueOf(number[1])); float result = operation.count(); System.out.println("结果是:" + result); } } /** * 运算类 * nunmber1 第一个运算符号 * nunmber2 第二个运算符号 * character 运算符号 */ class Operation { float number1; float number2; String character = null; public float getNumber1() { return number1; } public void setNumber1(float number1) { this.number1 = number1; } public float getNumber2() { return number2; } public void setNumber2(float number2) { this.number2 = number2; } public String getCharacter() { return character; } public void setCharacter(String character) { this.character = character; } public float count() { return 0; } } /** * 加法 */ class AddOperation extends Operation{ public float count() { return number1 + number2; } } /** * 减法 */ class SubOperation extends Operation{ public float count() { return number1 - number2; } } /** * 乘法 */ class MulOperation extends Operation{ public float count() { return number1 * number2; } } /** * 除法 */ class DivOperation extends Operation{ public float count() { return number1 / number2; } } /** * 创建运算类的工厂类 */ class OperationFactory { private static String[] characters = {"+","-","*","/"}; public Operation createOperate(String character) throws Exception { if(!this.checkCharacter(character)) { throw new Exception("运算符错误或不存在"); } switch(character) { case "+": return new AddOperation(); case "-": return new SubOperation(); case "*": return new MulOperation(); case "/": return new DivOperation(); } return null; } public String[] getCharacters() { return characters; } public void setCharacters(String[] characters) { this.characters = characters; } public static boolean checkCharacter(String character) { if(!Arrays.asList(characters).contains(character)) { return false; } return true; } } 请输入第一个数字:2 请输入符号(+、-、*、/):s 请输入符号(+、-、*、/):+ 请输入第二个数字:23 结果是:25.0
- 解决的问题:
- 前后端进行分离,前端负责获取需要运算的数据和运算方式
- 加减乘除运算类封装,便于其他程序使用
- 通过创建工厂类,让前端无需关注具体的类,只要正常的传入相应的数据就可以了
- 代码
- 过程式的开发
- 总结:简单工厂的作用就是,让前端尽可能的不参与到具体逻辑上,这样就达到了解耦的目的。
- 简单工厂的缺点:等学完抽象工厂再回来补
11-16
2603
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-28
451
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-27
1101
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)