抽象工厂模式
抽象工厂模式,让我们来继续前面的假设好了,我们知道,不管是洋娃娃还是小汽车,都需要电池的吧,不管精不精致,但是精致点的肯定电池也要好一点,而电池也有分不同的类型的,给洋娃娃用的电子电池,和给小气池用的充电电池,那么先从最简单的简单工厂说起,假设就一个厂,那么你得生产洋娃娃和小汽车,2种,精致的不精致的,4种了,再生产电子电池和充电电池,2种,贵的,便宜的,4种了,总共就8种,越来越臃肿了,肯定不行,那么我们来试试工厂模式,对生产的产品质量进行分类的话,分为精致厂和劣质厂,然后每个厂生产小汽车,洋娃娃,电子电池和充电电池。这样的话下面的具体实现厂还没问题,但是你的实现厂又显得臃肿了,这个时候抽象工厂就出现了,进行再洗牌,将你的精致厂和劣质厂下面再分,分为两个厂,分为小汽车厂和洋娃娃厂,而小汽车厂里面只生产小汽车和给小汽车用的充电电池,而洋娃娃厂里面只生产洋娃娃和给洋娃娃用的电子电池,这样的话,你就可以实现产品族的生产,而不是产品等级的生产了,这就是抽象工厂模式的特点了,但是这个是有一个缺点的,就是当你要再增加一个产品包装纸的时候,你就得两个厂都给增加一个生产的具体类,而对于工厂模式来说的话,你只需要再建一个生产纸就可以了。
下面是代码的实现,用的例子是CPU和主板的,CPU和主板有分为Intel品牌和Amd品牌,而Intel牌的CPU和Amd牌的主板是不兼容的,于是乎,我们可以购买的是一套的Intel牌子的CPU和主板;
先是核心类,抽象工厂接口类;
package abstractFactory; public interface AbstractFactory { public Cpu createCpu(); public Mainboard createMainboard(); }
然后是AMD公司的工厂类
package abstractFactory; public class AmdFactory implements AbstractFactory{ public AmdFactory() { // TODO Auto-generated constructor stub } @Override public Cpu createCpu(){ return new IntelCpu(938); } @Override public Mainboard createMainboard(){ return new IntelMainboard(938); } }
Intel公司的工厂类;
package abstractFactory; public class IntelFactory implements AbstractFactory{ public IntelFactory() { // TODO Auto-generated constructor stub } @Override public Cpu createCpu(){ return new IntelCpu(755); } @Override public Mainboard createMainboard(){ return new IntelMainboard(755); } }
CPU具体接口类
package abstractFactory; public interface Cpu { public void calculate(); }
主板具体接口类
package abstractFactory; public interface Mainboard { public void installCPU(); }
AMD公司下的产品具体实现类;
package abstractFactory; public class AmdCpu implements Cpu{ public AmdCpu() { // TODO Auto-generated constructor stub } private int pins = 0; public AmdCpu(int pins){ this.pins = pins; } @Override public void calculate(){ System.out.println("AMD CPU的针脚数: "+pins); } }
package abstractFactory; public class AmdMainboard implements Mainboard{ public AmdMainboard() { // TODO Auto-generated constructor stub } private int cpuHoles = 0; public AmdMainboard(int cpuHoles){ this.cpuHoles = cpuHoles; } @Override public void installCPU(){ System.out.println("AMD主板的CPU插槽孔数是"+cpuHoles); } }
Intel公司下的产品具体实现类;
package abstractFactory; public class IntelCpu implements Cpu{ public IntelCpu() { // TODO Auto-generated constructor stub } private int pins = 0; public IntelCpu(int pins){ this.pins = pins; } @Override public void calculate(){ System.out.println("Intel CPU的针脚数 "+pins); } }
package abstractFactory; public class IntelMainboard implements Mainboard{ public IntelMainboard() { // TODO Auto-generated constructor stub } private int cpuHoles = 0; public IntelMainboard(int cpuHoles){ this.cpuHoles = cpuHoles; } @Override public void installCPU(){ System.out.println("Intel主板的CPU插槽孔数是:" +cpuHoles); } }
下面是测试类;
package abstractFactory; public class ComputerEngineer { public ComputerEngineer() { // TODO Auto-generated constructor stub } private Cpu cpu = null; private Mainboard mainboard = null; public void makeComputer(AbstractFactory af){ prepareHardwares(af); } private void prepareHardwares(AbstractFactory af){ this.cpu = af.createCpu(); this.mainboard = af.createMainboard(); this.cpu.calculate(); this.mainboard.installCPU(); } }
package abstractFactory; public class Client { public Client() { // TODO Auto-generated constructor stub } public static void main(String[] args){ ComputerEngineer cf = new ComputerEngineer(); AbstractFactory af = new IntelFactory(); cf.makeComputer(af); } }