简单工厂模式
以实现加、减、乘、除的计算器功能为例
初级代码:
输入数字交互界面
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.println("**********************************************");
System.out.println("《大话设计模式》代码样例");
System.out.println();
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字A:");
double numberA = Double.parseDouble(sc.nextLine());
System.out.println("请选择运算符号(+、-、*、/):");
String strOperate = sc.nextLine();
System.out.println("请输入数字B:");
double numberB = Double.parseDouble(sc.nextLine());
double result = getResult(numberA, numberB, strOperate);
System.out.println("结果是:" + result);
} catch (Exception e) {
System.out.println("您的输入有错:" + e.toString());
}
System.out.println();
System.out.println("**********************************************");
}
运算方法:
public static double getResult(double numberA, double numberB, String operate) {
double result = 0d;
switch (operate) {
case "+":
result = numberA + numberB;
break;
case "-":
result = numberA - numberB;
break;
case "*":
result = numberA * numberB;
break;
case "/":
result = numberA / numberB;
break;
case "pow":
result = java.lang.Math.pow(numberA, numberB);
break;
}
return result;
}
缺陷:无法做到灵活的可修改和扩展
比如说加个其他运算,导致需要重新修改运算方法 且需要把加减乘除的代码重新编译一遍;且如果不小心改了其他方法,程序会出现问题,给程序带来了风险;
好的代码应该是,修改其中一个不影响另外的几个,增加运算算法也不影响其它代码;
高级写法:
思路:
到底要实例化谁,将来会不会增加实例化的对象,也就是说会不会增加运算符,这是很容易变化的地方,应该考虑用一个单独的类来做这个创造实例的过程,这就是工厂
classDiagram class Operation{ <<abstract>> +double numberA +double numberB +double getResult() } class OperationAdd{ +double getResult() } class OperationSub{ +double getResult() } class OperationFactory { +Operation createOperation(String operator) } class interactionView { main(String[] args) } interactionView o-- OperationFactory : createOperation OperationFactory o-- Operation Operation <|-- OperationAdd Operation <|-- OperationSub输入数字交互界面:
public static void main(String[] args){
System.out.println("**********************************************");
System.out.println("《大话设计模式》代码样例");
System.out.println();
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字A:");
double numberA = Double.parseDouble(sc.nextLine());
System.out.println("请选择运算符号(+、-、*、/):");
String strOperate = sc.nextLine();
System.out.println("请输入数字B:");
double numberB = Double.parseDouble(sc.nextLine());
Operation oper = OperationFactory.createOperate(strOperate);
double result = oper.getResult(numberA,numberB);
System.out.println("结果是:"+result);
}
catch(Exception e){
System.out.println("您的输入有错:"+e.toString());
}
System.out.println();
System.out.println("**********************************************");
}
工厂类
public class OperationFactory {
public static Operation createOperate(String operate){
Operation oper = null;
switch (operate) {
case "+":
oper = new Add();
break;
case "-":
oper = new Sub();
break;
case "*":
oper = new Mul();
break;
case "/":
oper = new Div();
break;
}
return oper;
}
}
抽象化运算类
public abstract class Operation {
public double getResult(double numberA, double numberB){
return 0d;
}
}
加
public class Add extends Operation {
public double getResult(double numberA, double numberB){
return numberA + numberB;
}
}
标签:numberB,numberA,double,大话,System,笔记,println,设计模式,out
From: https://www.cnblogs.com/lixiuming521125/p/17772998.html