简单工厂模式:
又叫静态工厂方法模式,是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的类。
工厂模式:
对对象的创建进行封装,用户不需要知道具体的创建过程。在实际使用过程中,违背了开放-关闭原则,当然有些情况下可以通过反射调用来弥补这种不足。
抽象工厂模式:
提供一个创建——系列相关相互依赖对象的接口,而无需指定他们具体的类。抽象工厂为不同产品族的对象创建提供接口。
以下是代码实现:
原始代码实现加减乘除:
public class Computer { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入第一个数字:"); float firstNum = in.nextFloat(); System.out.println("请输入第二个数字:"); float secondNum = in.nextFloat(); System.out.println("请输入运算符号:"); String countQuato = in.next(); if("+".equals(countQuato)){ System.out.println("result : "+(firstNum+secondNum)); }else if("-".equals(countQuato)){ System.out.println("result : "+(firstNum-secondNum)); }else if("*".equals(countQuato)){ System.out.println("result : "+(firstNum*secondNum)); }else if("/".equals(countQuato)){ System.out.println("result : "+(firstNum/secondNum)); } } }
上面的写法实现虽然简单,但是没有面向对象的特性,代码拓展性差。
简单工厂模式:
在面向对象编程语言中,一切都是对象,所以上面运算符号也应当作对象来处理。
简单工厂将对象的创建过程进行了封装,用户不需要知道具体的创建过程,只需要调用工厂类获取对象即可。
public abstract class Operation { public abstract float getResult(float firstNumber, float secondNumber); }
public class AddOperation extends Operation { @Override public float getResult(float firstNumber, float secondNumber) { return firstNumber + secondNumber; } }
public class SubOperation extends Operation { @Override public float getResult(float firstNumber, float secondNumber) { return firstNumber - secondNumber; } }
public class MulOperation extends Operation { @Override public float getResult(float firstNumber, float secondNumber) { return firstNumber * secondNumber; } }
public class DivOperation extends Operation { @Override public float getResult(float firstNumber, float secondNumber) { return firstNumber / secondNumber; } }
public class OperationFactory { public static Operation getOperation(String quotaFlag){ Operation o = null; switch (quotaFlag){ case "+" : o = new AddOperation();break; case "-" : o = new SubOperation();break; case "*" : o = new MulOperation();break; case "/" : o = new DivOperation();break; default:break; } return o; } }
import java.util.Scanner; public class Computer { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入第一个数字:"); float firstNum = in.nextFloat(); System.out.println("请输入第二个数字:"); float secondNum = in.nextFloat(); System.out.println("请输入运算符号:"); String countQuato = in.next(); System.out.println("result : " + count(firstNum, secondNum, countQuato)); } private static float count(float firstNum, float secondNum, String countQuato) { Operation operation = OperationFactory.getOperation(countQuato); return operation.getResult(firstNum, secondNum); } }
工厂方法:
工厂方法定义一个用于创建对象的接口,让子类决定实例化哪个类,工厂方法使得一个类的 实例化延迟到子类。工厂方法在简单工厂的基础上再包了一层工厂,所有的工厂都是此工厂的子类。而产生对象 的类型由子类工厂决定。
/** * 定义一个统一的操作工厂接口 */ public interface IFactory { Operation generateOper(); }
public class AddOperationFactory implements IFactory { @Override public Operation generateOper() { return new AddOperation(); } }
public class SubOperationFactory implements IFactory { @Override public Operation generateOper() { return new SubOperation(); } }
public class MulOperationFactory implements IFactory { @Override public Operation generateOper() { return new MulOperation(); } }
public class DivOperationFactory implements IFactory { @Override public Operation generateOper() { return new DivOperation(); } }
import java.util.Scanner; public class Computer { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入第一个数字:"); float firstNum = in.nextFloat(); System.out.println("请输入第二个数字:"); float secondNum = in.nextFloat(); System.out.println("请输入运算符号:"); String countQuato = in.next(); System.out.println("result : " + count(firstNum, secondNum, countQuato)); } private static float count(float firstNum, float secondNum, String countQuato) { // Operation operation = OperationFactory.getOperation(countQuato); IFactory factory = new AddOperationFactory(); Operation operation = factory.generateOper(); return operation.getResult(firstNum, secondNum); } }
抽象工厂模式:
提供一个创建——系列相关相互依赖对象的接口,而无需指定他们的具体的类。抽象工厂为不同产品族的对象创建提供的接口。
package factory; import factory.cpu.Cpu; import factory.mainboard.Mainboard; /** * 定义一个cpu 和 mianboard 的抽象工厂 */ public interface AbastractFactory { Cpu createCpu(); Mainboard createMainboard(); }
package factory; import factory.cpu.Cpu; import factory.cpu.IntelCpu; import factory.mainboard.IntelMainboard; import factory.mainboard.Mainboard; public class IntelFactory implements AbastractFactory { @Override public Cpu createCpu() { return new IntelCpu(755); } @Override public Mainboard createMainboard() { return new IntelMainboard(938); } }
package factory; import factory.cpu.AmdCpu; import factory.cpu.Cpu; import factory.mainboard.AmdMainboard; import factory.mainboard.Mainboard; public class AmdFactory implements AbastractFactory { @Override public Cpu createCpu() { return new AmdCpu(755); } @Override public Mainboard createMainboard() { return new AmdMainboard(938); } }
package factory.cpu; public interface Cpu { void calculate(); }
package factory.cpu; public class AmdCpu implements Cpu { //CPU的针脚数 private int pins = 0; public AmdCpu(int pins) { this.pins = pins; } @Override public void calculate() { System.out.println("AMD CPU的针脚数:" + pins); } }
package factory.cpu; public class IntelCpu implements Cpu { //CPU的针脚数 private int pins = 0; public IntelCpu(int pins) { this.pins = pins; } @Override public void calculate() { System.out.println("Intel CPU的针脚数:" + pins); } }
package factory.mainboard; public interface Mainboard { void installCPU(); }
package factory.mainboard; public class AmdMainboard implements Mainboard { //CPU插槽的孔数 private int cpuHoles = 0; public AmdMainboard(int cpuHoles) { this.cpuHoles = cpuHoles; } @Override public void installCPU() { System.out.println("AMD主板的CPU插槽孔数是:" + cpuHoles); } }
package factory; import factory.cpu.Cpu; import factory.mainboard.Mainboard; public class Client { public static void main(String[] args) { AbastractFactory factory = new IntelFactory(); Cpu cpu = factory.createCpu(); cpu.calculate(); Mainboard mainboard = factory.createMainboard(); mainboard.installCPU(); } }
抽象工厂模式的优点:
1、便于交换产品系列,改变应用的具体工厂容易;
2、创建对象和客户端分离,客户端是通过他们的抽象接口操作实例
简单的工厂模式——工厂方法——抽象工厂模式:
这三个模式都是客户端的需求不断的增加,一步一步演进而来。