抽象工厂,提供一个工厂接口,用于创建依赖对象的家族,不需要明确指定具体类。可以理解是多个工厂方法的组合。
例如一个款手机是有电池、相机、处理器、屏幕组成的,它其中包含了不同的对象,我们创建时需要创建多个不同类型的对象,而工厂方法只能创建一种类型的对象,满足不了这种需求。那我们可以把多个工厂方法组合到一起,用来创建多个产品。
首先我们先定义抽象工厂,他可以生产电池、相机、处理器、屏幕
public interface BasePhoneFactory { public BaseCamara createCamara(); public BaseBattery createBattery(); public BaseScreen createScreen(); public BaseCpu createCpu(); }
然后创建各个抽象的产品
public interface BaseBattery { public void name(); } public interface BaseCamara { public void name(); } public interface BaseCpu { public void name(); } public interface BaseScreen { public void name(); }
然后各个产品的实现类
public class Battery_3000 implements BaseBattery { @Override public void name() { System.out.println("3000毫安锂电池"); } } public class Battery_3800 implements BaseBattery { @Override public void name() { System.out.println("3800毫安锂电池"); } } public class Camara_1200 implements BaseCamara { @Override public void name() { System.out.println("1200W像素相机"); } } public class Camara_1800 implements BaseCamara { @Override public void name() { System.out.println("1800W像素相机"); } } public class Cpu_4_8 implements BaseCpu { @Override public void name() { System.out.println("4核8线程处理器"); } } public class Cpu_6_12 implements BaseCpu { @Override public void name() { System.out.println("6核12线程处理器"); } } public class Screen_1080 implements BaseScreen { @Override public void name() { System.out.println("1080P屏幕"); } } public class Screen_1440 implements BaseScreen { @Override public void name() { System.out.println("1440P屏幕"); } }
现在就是我们工厂的创建了
public class PhoneFactory_2018 implements BasePhoneFactory { @Override public BaseCamara createCamara() { return new Camara_1200(); } @Override public BaseBattery createBattery() { return new Battery_3000(); } @Override public BaseScreen createScreen() { return new Screen_1080(); } @Override public BaseCpu createCpu() { return new Cpu_4_8(); } } public class PhoneFactory_2019 implements BasePhoneFactory { @Override public BaseCamara createCamara() { return new Camara_1800(); } @Override public BaseBattery createBattery() { return new Battery_3800(); } @Override public BaseScreen createScreen() { return new Screen_1440(); } @Override public BaseCpu createCpu() { return new Cpu_6_12(); } }
下面是手机基类和它的实现类
public abstract class BasePhone { /** * 电池 */ protected BaseBattery battery; /** * 相机 */ protected BaseCamara camara; /** * 处理器 */ protected BaseCpu cpu; /** * 屏幕 */ protected BaseScreen screen; public void display() { battery.name(); camara.name(); cpu.name(); screen.name(); } public abstract void getPhone(); }
public class Phone_2018 extends BasePhone { BasePhoneFactory phoneFactory; public Phone_2018() { phoneFactory = new PhoneFactory_2018(); } @Override public void getPhone() { this.camara = this.phoneFactory.createCamara(); this.battery = this.phoneFactory.createBattery(); this.cpu = this.phoneFactory.createCpu(); this.screen = this.phoneFactory.createScreen(); } @Override public void display() { System.out.println("2018年款手机"); super.display(); } }
public class Phone_2019 extends BasePhone { BasePhoneFactory phoneFactory; public Phone_2019() { phoneFactory = new PhoneFactory_2019(); } @Override public void getPhone() { this.camara = this.phoneFactory.createCamara(); this.battery = this.phoneFactory.createBattery(); this.cpu = this.phoneFactory.createCpu(); this.screen = this.phoneFactory.createScreen(); } @Override public void display() { System.out.println("2019年款手机"); super.display(); } }
测试代码
public class Main { public static void main(String[] args) { BasePhone phone1 = new Phone_2018(); phone1.getPhone(); phone1.display(); System.out.println("-------------------"); BasePhone phone2 = new Phone_2019(); phone2.getPhone(); phone2.display(); } }
输出结果
抽象工厂一个具体的工厂就能创建出多个产品,和工厂方法相比,减少了具体工厂的数量。
但是,如果需要给抽象工厂中添加一个成员,就需要改动所有的工厂,扩展性就比较差了。