简单工厂模式(Simple Factory)属于创建型模式,其不属于23种设计模式之中,但是我还是认为有必要来学习一下,就简单记录一下。
首先我们就以一个简单的计算器功能为例,如果我们不考虑任何设计去实现可能是如下:
public class Test {
public static void main(String[] args) {
System.out.println("请输入一个数字");
Scanner scanner = new Scanner(System.in);
String numA = scanner.next();
System.out.println("请输入运算符");
String operate = scanner.next();
System.out.println("请输入一个数字");
String numB = scanner.next();
if("+".equals(operate)) {
System.out.println(Double.valueOf(numA)+Double.valueOf(numB));
}
if("-".equals(operate)) {
System.out.println(Double.valueOf(numA)-Double.valueOf(numB));
}
if("*".equals(operate)) {
System.out.println(Double.valueOf(numA)*Double.valueOf(numB));
}
if("/".equals(operate)) {
System.out.println(Double.valueOf(numA)/Double.valueOf(numB));
}
}
}
这种代码写法显然是实现了简单的功能 ,但是我们看着真的是毫无设计可言,而且扩展性极低,如果我们将其面向对象进行设计一下可能会比现在好很多,如下:
运算操作类
public class Operation {
public static double getResult(double numA,double numB,String operate) {
double result = 0;
switch (operate) {
case "+":
result = numA + numB;
break;
case "-":
result = numA - numB;
break;
case "*":
result = numA * numB;
break;
case "/":
result = numA / numB;
break;
}
return result;
}
}
public class Home {
public static void main(String[] args) {
System.out.println("请输入一个数字");
Scanner scanner = new Scanner(System.in);
String numA = scanner.next();
System.out.println("请输入运算符");
String operate = scanner.next();
System.out.println("请输入一个数字");
String numB = scanner.next();
double result = Operation.getResult(Double.valueOf(numA), Double.valueOf(numB), operate);
System.out.println("结果是:"+result);
}
}
这样将主程序和运算分开来,基本符合了面向对象的编程思想,但是这样写的话代码的扩展性还是很低的,后期的维护和修改都容易出错,所以接下来我们用简单工厂模式来实现以下:
抽象类,为了让其子类继承并且重写getResult()方法
public abstract class Operation {
protected double numberA = 0;
protected double numberB = 0;
protected double result;
public abstract double getResult();
public Operation() {
super();
// TODO Auto-generated constructor stub
}
public double getNumberA() {
return numberA;
}
public void setNumberA(double numberA) {
this.numberA = numberA;
}
public double getNumberB() {
return numberB;
}
public void setNumberB(double numberB) {
this.numberB = numberB;
}
}
其各子类
public class OperationAdd extends Operation{
@Override
public double getResult() {
result = numberA + numberB;
return result;
}
}
public class OperationSub extends Operation{
@Override
public double getResult() {
result = numberA - numberB;
return result;
}
}
public class OperationMul extends Operation{
@Override
public double getResult() {
result = numberA * numberB;
return result;
}
}
public class OperationDiv extends Operation{
@Override
public double getResult() {
result = numberA / numberB;
return result;
}
}
工厂类
public class OperationFactory {
public static Operation createOperate(String operate) {
Operation operation = null;
switch (operate) {
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
}
return operation;
}
}
客户端
public class Home {
public static void main(String[] args) {
Operation op;
op = OperationFactory.createOperate("+");
op.setNumberA(22);
op.setNumberB(33);
System.out.println(op.getResult());
}
}
这样我们就实现了一个简单工厂模式的计算器功能,这样写与之前的方法比起来扩展性更强,如果加入别的运算我们只需要再多实现一个子类,然后到工厂类中加一个判断就可以了,易于维护。
实现简单工厂模式我们只需要有一个工厂类,一个抽象类还有其子类去实现不同的方法即可。
以上只是我今天学习之后的一个简单理解,记在这里,大家有不同意见和想法可以留言沟通讨论。后面还会继续更新其他设计模式。