简单工厂模式

工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的。

工厂模式有三种:简单工厂模式、工厂方法模式、抽象工厂模式

这里先介绍简单工厂模式

简单工厂模式

又称静态工厂方法模式,从名字来看,这种模式体现的就是简单。主要的实现思想是通过创建一个工厂类,通过调用工厂方法的静态方法返回相应的对象。例如有一个计算程序:输入两个数和运算符号(+-*/),输出运算结果。可以这样写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数:");
String number1 = scanner.next();
System.out.println("请输入第二个数:");
String number2 = scanner.next();
System.out.println("请输入运算符号:");
String operation = scanner.next();
switch (operation){
case "+":
System.out.println(Integer.valueOf(number1) + Integer.valueOf(number2));
break;
case "-":
System.out.println(Integer.valueOf(number1) - Integer.valueOf(number2));
break;
case "*":
System.out.println(Integer.valueOf(number1) * Integer.valueOf(number2));
break;
case "/":
System.out.println(Integer.valueOf(number1) / Integer.valueOf(number2));
break;
}
scanner.close();

当然,这只是最简单的实现,并且没有对除数为0等做处理,这样做也行,但如果后面要添加更多的计算方法,比如开方、乘方等,那么就要无限的增加switch分支,让程序难以维护。所以可以采用简单工厂的方式实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public abstract class Operation {
private double number1 = 0;
private double number2 = 0;
public double getNumber1() {
return number1;
}
public void setNumber1(double number1) {
this.number1 = number1;
}
public double getNumber2() {
return number2;
}
public void setNumber2(double number2) {
this.number2 = number2;
}
public abstract double getResult();
}

运算抽象类,后面的运算规则通过继承这个类进行运算。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class AddOperation extends Operation {
@Override
public double getResult() {
return getNumber1() + getNumber2();
}
}
public class SubOperation extends Operation {
@Override
public double getResult() {
return getNumber1() - getNumber2();
}
}
public class MulOperation extends Operation {
@Override
public double getResult() {
return getNumber1() * getNumber2();
}
}
public class DivOperation extends Operation {
@Override
public double getResult() {
return getNumber1() / getNumber2();
}
}

定义一个构造计算方法的工厂:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class OperationFactory {
public static Operation createOperation(char operate){
Operation operation = null;
switch (operate){
case '+':
operation = new AddOperation();
break;
case '-':
operation = new SubOperation();
break;
case '*':
operation = new MulOperation();
break;
case '/':
operation = new DivOperation();
break;
}
return operation;
}
}

这样就能通过这个简单工厂来创建相应的运算对象(当然,有很多细节没有进行处理)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数:");
String number1 = scanner.next();
System.out.println("请输入第二个数:");
String number2 = scanner.next();
System.out.println("请输入运算符号:");
String operation = scanner.next();
Operation o = null;
try {
o = OperationFactory.createOperation(operation.charAt(0));
o.setNumber1(Double.valueOf(number1));
o.setNumber2(Double.valueOf(number2));
}catch (Exception e){
}
System.out.println(o.getResult());

文章目录
  1. 1. 简单工厂模式
|