设计模式-创建型-抽象工厂

143 阅读2分钟

简介

抽象工厂是工厂模式的一种,属于创建型模式。对于简单工厂和工厂方法,其工厂类主要是创建一个单纯的产品,如苹果工厂只负责创建苹果,南方苹果工厂只负责创建南方的苹果。抽象工厂主要是对于产品族,如我们手机,我们抽象里面有CPU和充电器,而手机目前有Iphone和Android两种,Iphone的CPU/充电器不能应用于Android。如果我们要创建一部手机,如果用工厂方法创建一个Iphone,我们需要Iphone的CPU和Charge工厂,然后再创建一个Iphone工厂,在Iphone工厂里调用Iphone的Cup和Charge工厂里组装成一个Iphone,伪码如下:

public class IphoneFactory {
    public static Phone createIphone() {
        CPU cpu = CpuFactory.createCpu("iphone");
        Charge charge = ChargeFactory.createCharge("iphone");
        Phone phone = new Phone(cpu, charge);
        return phone;
    }
}

抽象工厂

使用抽象工厂模式,其主要聚焦在产品族上,例如上面的例子,其聚焦于Iphone和Android的创建。其具体实现如下: 1.首先定义组成Iphone和Android组件的接口(这里只抽象了CPU和Charge),然后Iphone和Android的CPU来实现它。

public interface Cpu {
    String getCpuType();
}

public class AndrodCpu implements Cpu{
    @Override
    public String getCpuType() {
        return "android cpu";
    }
}

public class IphoneCpu implements Cpu{
    @Override
    public String getCpuType() {
        return "iphone cpu";
    }
}
public interface Charge {
    String getChargeType();
}

public class AndroidCharge implements Charge{
    @Override
    public String getChargeType() {
        return "android charge";
    }
}

public class IphoneCharge implements Charge{

    @Override
    public String getChargeType() {
        return "iphone charge";
    }
}

2.定义创建phone的工厂接口,Iphone和Android分别实现它。

public interface PhoneFactory {
    Cpu createCpu();

    Charge createCharge();
}

public class IphoneFactory implements PhoneFactory{
    @Override
    public Cpu createCpu() {
        return new IphoneCpu();
    }

    @Override
    public Charge createCharge() {
        return new IphoneCharge();
    }
}

public class AndroidFactory implements PhoneFactory{
    @Override
    public Cpu createCpu() {
        return new AndrodCpu();
    }

    @Override
    public Charge createCharge() {
        return new AndroidCharge();
    }
}

3.定义Phone类

public class Phone {

    private Cpu cpu;
    private Charge charge;

    public Phone(Cpu cpu, Charge charge) {
        this.cpu = cpu;
        this.charge = charge;
    }

    public Cpu getCpu() {
        return cpu;
    }

    public void setCpu(Cpu cpu) {
        this.cpu = cpu;
    }

    public Charge getCharge() {
        return charge;
    }

    public void setCharge(Charge charge) {
        this.charge = charge;
    }

    @Override
    public String toString() {
        return "Phone{" +
                "cpu=" + cpu.getCpuType() +
                ", charge=" + charge.getChargeType() +
                '}';
    }
}

4.客户端调用

public class Test {
    public static void main(String[] args) {
        PhoneFactory iPhoneFactory = new IphoneFactory();
        Cpu iphoneCup = iPhoneFactory.createCpu();
        Charge iphoneCharge = iPhoneFactory.createCharge();
        Phone phone = new Phone(iphoneCup, iphoneCharge);
        System.out.println(phone);//Phone{cpu=iphone cpu, charge=iphone charge}

        PhoneFactory androidFactory = new AndroidFactory();
        Cpu androidCpu = androidFactory.createCpu();
        Charge androidCharge = androidFactory.createCharge();
        Phone phone1 = new Phone(androidCpu, androidCharge);
        System.out.println(phone1);//Phone{cpu=android cpu, charge=android charge}
    }
}

总结

从上面的Demo中可以看出,对于需要创建的CPU和Charge,并没有相对应的CPUFactory和ChargeFactory,而是交给了产品的factory来创建。这也正是抽象工厂的价值所在,避免的客户端调用的出错,让客户端调用更方便,更清晰。