工厂模式:
将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目依赖关系的解耦,从而提高项目的扩展性和维护性。
简单工厂模式:
工厂类中包含了必要的逻辑,有工厂类中的选择条件来判断实例化相关的类。
接口类:
public interface Operation {
Long operate(long a, long b);
}
实现类:
public class AddOperation implements Operation{
@Override
public Long operate(long a, long b) {
return a + b;
}
}
public class SubOperation implements Operation{
@Override
public Long operate(long a, long b) {
return a - b;
}
}
public class MulOperation implements Operation{
@Override
public Long operate(long a, long b) {
return a * b;
}
}
public class DivOperation implements Operation{
@Override
public Long operate(long a, long b) {
return a / b;
}
}
工厂类:
public class OperationFactory {
public static Operation createOperation(String 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;
}
public static void main(String[] args) throws IOException {
System.out.println("请输入第一个数");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String firstNumber = reader.readLine();
System.out.println("请输入运算符");
reader = new BufferedReader(new InputStreamReader(System.in));
String operate = reader.readLine();
System.out.println("请输入第二个数");
reader = new BufferedReader(new InputStreamReader(System.in));
String secondNumer = reader.readLine();
Operation operation = OperationFactory.createOperation(operate);
Long result = operation.operate(Long.valueOf(firstNumber), Long.valueOf(secondNumer));
System.out.println(result);
}
}
静态工厂模式:
就上面简单工厂模式的代码,区别就在于工厂类的方法是否为静态。
工厂方法模式:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。简单来说,就是有多个工厂实现类对应相应的逻辑实现类,将实例化的逻辑判断移到了调用方。
业务类接口:
public abstract class Operation {
protected long a;
protected long b;
abstract Long getResult();
}
业务类实现:
public class AddOperation extends Operation {
@Override
public Long getResult() {
return a + b;
}
}
public class SubOperation extends Operation {
@Override
Long getResult() {
return a - b;
}
}
工厂接口:
public interface OperationFactory {
Operation createOperation();
}
工厂实现类:
public class AddOperationFactory implements OperationFactory {
@Override
public Operation createOperation() {
return new AddOperation();
}
}
public class SubOperationFactory implements OperationFactory{
@Override
public Operation createOperation() {
return new SubOperation();
}
}
测试类:
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("请输入第一个数");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String firstNumber = reader.readLine();
System.out.println("请输入第二个数");
reader = new BufferedReader(new InputStreamReader(System.in));
String secondNumer = reader.readLine();
Operation operation = new SubOperation();
operation.a = Long.valueOf(firstNumber);
operation.b = Long.valueOf(secondNumer);
System.out.println(operation.getResult());
}
}
抽象工厂模式:
在工厂方法模式上再抽象了一层,将之前一个单一工厂类的职责增加为多个。用于多维度的业务实现。
代码与之前工厂方法模式类似,在工厂类中又加了一层实现。