设计模式 | 简单工厂模式
1 package designpattern.staticfactorymethod; 2 import java.util.Scanner; 3 public class Calculator { 4 public static void main(String[] args) { 5 Scanner scanner = new Scanner(System.in); 6 System.out.println("请输入一个数字"); 7 double num1 = scanner.nextDouble(); 8 System.out.println("请输入一个运算符:+、-、*、/"); 9 String operator = scanner.next();// 不能用nextLine(),会把上一个回车给吸收 10 System.out.println("请输入一个数字"); 11 double num2 = scanner.nextDouble(); 12 switch (operator) { 13 case "+": 14 System.out.println(num1 + num2); 15 break; 16 case "-": 17 System.out.println(num1 - num2); 18 break; 19 case "*": 20 System.out.println(num1 * num2); 21 break; 22 case "/": 23 System.out.println(num1 / num2); 24 break; 25 default: 26 break; 27 } 28 scanner.close(); 29 } 30 }
最多把计算的方法单拉出来:
package designpattern.staticfactorymethod; import java.util.Scanner; public class Calculator2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个数字"); double num1 = scanner.nextDouble(); System.out.println("请输入一个运算符:+、-、*、/"); String operator = scanner.next();// 不能用nextLine(),会把上一个回车给吸收 System.out.println("请输入一个数字"); double num2 = scanner.nextDouble(); switch (operator) { case "+": System.out.println(plus(num1, num2)); break; case "-": System.out.println(minus(num1, num2)); break; case "*": System.out.println(multiply(num1, num2)); break; case "/": System.out.println(divide(num1, num2)); break; default: break; } scanner.close(); } static double plus(double num1, double num2) { return num1 + num2; } static double minus(double num1, double num2) { return num1 - num2; } static double multiply(double num1, double num2) { return num1 * num2; } static double divide(double num1, double num2) { return num1 / num2; } }
package designpattern.staticfactorymethod; public class CalculatorUtil { static double plus(double num1, double num2) { return num1 + num2; } static double minus(double num1, double num2) { return num1 - num2; } static double multiply(double num1, double num2) { return num1 * num2; } static double divide(double num1, double num2) { return num1 / num2; } }
package designpattern.staticfactorymethod; import java.util.Scanner; public class Calculator3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个数字"); double num1 = scanner.nextDouble(); System.out.println("请输入一个运算符:+、-、*、/"); String operator = scanner.next();// 不能用nextLine(),会把上一个回车给吸收 System.out.println("请输入一个数字"); double num2 = scanner.nextDouble(); Calculator3 calculator3 = new Calculator3(); switch (operator) { case "+": System.out.println(calculator3.new Plus(num1, num2).calculate()); break; case "-": System.out.println(calculator3.new Minus(num1, num2).calculate()); break; case "*": System.out.println(calculator3.new Multiply(num1, num2).calculate()); break; case "/": System.out.println(calculator3.new Divide(num1, num2).calculate()); break; default: break; } scanner.close(); } class Plus { double num1; double num2; Plus(double num1, double num2) { this.num1 = num1; this.num2 = num2; } double calculate() { return num1 + num2; } } class Minus { double num1; double num2; Minus(double num1, double num2) { this.num1 = num1; this.num2 = num2; } double calculate() { return num1 - num2; } } class Multiply { double num1; double num2; Multiply(double num1, double num2) { this.num1 = num1; this.num2 = num2; } double calculate() { return num1 * num2; } } class Divide { double num1; double num2; Divide(double num1, double num2) { this.num1 = num1; this.num2 = num2; } double calculate() { return num1 / num2; } } }
package designpattern.staticfactorymethod; public abstract class Calculate { double num1; double num2; Calculate() { } Calculate(double num1, double num2) { this.num1 = num1; this.num2 = num2; } public abstract double calculate(); }
具体运算子类们:
package designpattern.staticfactorymethod; public class Plus extends Calculate { Plus() { } Plus(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 + num2; } }
package designpattern.staticfactorymethod; public class Minus extends Calculate { Minus() { } Minus(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 - num2; } }
package designpattern.staticfactorymethod; public class Multiply extends Calculate { Multiply() { } Multiply(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 * num2; } }
package designpattern.staticfactorymethod; public class Divide extends Calculate { Divide() { } Divide(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 / num2; } }
当然实际代码中,不能像上面这样天马行空,术业还是要有专攻的,比如一个工厂专门生产各种各样的女朋友,额。。
比如iphon的工厂专门生产各种型号的ipone。
把这个思想用到我们的程序中,我们需要一个工厂,接受一个参数,然后它就能返回相应的对象:
package designpattern.staticfactorymethod; public class CalculateFactory { public static Calculate create(String operate) { switch (operate) { case "+": return new Plus(); case "-": return new Minus(); case "*": return new Multiply(); case "/": return new Divide(); } return null; } }
package designpattern.staticfactorymethod; import java.util.Scanner; public class Calculator4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个数字"); double num1 = scanner.nextDouble(); System.out.println("请输入一个运算符:+、-、*、/"); String operator = scanner.next();// 不能用nextLine(),会把上一个回车给吸收 System.out.println("请输入一个数字"); double num2 = scanner.nextDouble(); Calculate calculate = CalculateFactory.create(operator); calculate.num1 = num1; calculate.num2 = num2; System.out.println(calculate.calculate()); scanner.close(); } }
这回多态也出场了,通过多态,统一用父类接受创建的具体子类,当需要增加运算方式时,只需要修改工厂类和具体的运算类,调用的地方不用变,降低了模块间的耦合性,提高了系统的灵活性。