抽象工厂模式(Abstract Factory Pattern)是一种创建型设计模式,它提供了一种创建一系列相关或相互依赖对象的最佳方式,而无需指定它们具体的类。抽象工厂模式使用一组相关的工厂方法来创建一组对象,这些工厂方法构成了一个工厂族,它们通常用于创建复杂对象或对象组。

抽象工厂模式与工厂方法模式的主要区别在于,抽象工厂模式针对的是产品族,而工厂方法模式针对的是产品等级结构。抽象工厂模式中的每个具体工厂类负责创建一整组产品,而不是单一的产品。

下面以一个简单的例子来说明抽象工厂模式的应用。

假设我们需要创建一个电子设备的抽象工厂,该工厂可以创建电视机、音响和电脑这三种产品,并且有三种不同的工厂实现,分别对应不同的品牌:华为、小米和苹果。对于每个工厂而言,它都可以生产对应品牌的所有产品。

首先,我们定义三个电子设备产品接口:

public interface Television {
    void play();
}

public interface Audio {
    void play();
}

public interface Computer {
    void play();
}

然后,我们定义三个不同品牌的具体产品类,分别实现电子设备产品接口:

// 华为电视机、音响和电脑
public class HuaweiTelevision implements Television {
    public void play() {
        System.out.println("Playing on Huawei Television");
    }
}

public class HuaweiAudio implements Audio {
    public void play() {
        System.out.println("Playing on Huawei Audio");
    }
}

public class HuaweiComputer implements Computer {
    public void play() {
        System.out.println("Playing on Huawei Computer");
    }
}

// 小米电视机、音响和电脑
public class XiaomiTelevision implements Television {
    public void play() {
        System.out.println("Playing on Xiaomi Television");
    }
}

public class XiaomiAudio implements Audio {
    public void play() {
        System.out.println("Playing on Xiaomi Audio");
    }
}

public class XiaomiComputer implements Computer {
    public void play() {
        System.out.println("Playing on Xiaomi Computer");
    }
}

// 苹果电视机、音响和电脑
public class AppleTelevision implements Television {
    public void play() {
        System.out.println("Playing on Apple Television");
    }
}

public class AppleAudio implements Audio {
    public void play() {
        System.out.println("Playing on Apple Audio");
    }
}

public class AppleComputer implements Computer {
    public void play() {
        System.out.println("Playing on Apple Computer");
    }
}

接下来,我们定义三个工厂接口,分别用于生产不同品牌的电子设备:

public interface HuaweiFactory {
    Television createTelevision();
    Audio createAudio();
    Computer createComputer();
}

public interface XiaomiFactory {
    Television createTelevision();
    Audio createAudio();
    Computer createComputer();
}

public interface AppleFactory {
    Television createTelevision();
    Audio createAudio();
    Computer createComputer();
}

最后,我们实现这三个工厂接口,分别用于生产对应品牌的电子设备产品:

// 华为工厂
public class HuaweiElectronicsFactory implements HuaweiFactory {
    public Television createTelevision() {
        return new HuaweiTelevision();
    }

    public Audio createAudio() {
        return new HuaweiAudio();
    }

    public Computer createComputer() {
        return new HuaweiComputer();
    }
}

// 小米工厂
public class XiaomiElectronicsFactory implements XiaomiFactory {
    public Television createTelevision() {
        return new XiaomiTelevision();
    }

    public Audio createAudio() {
        return new XiaomiAudio();
    }

    public Computer createComputer() {
        return new XiaomiComputer();
    }
}

// 苹果工厂
public class AppleElectronicsFactory implements AppleFactory {
    public Television createTelevision() {
        return new AppleTelevision();
    }

    public Audio createAudio() {
        return new AppleAudio();
    }

    public Computer createComputer() {
        return new AppleComputer();
    }
}

现在我们已经完成了抽象工厂模式的实现。我们可以在客户端中创建不同品牌的工厂,然后使用这些工厂生产对应品牌的电子设备产品。下面是一个示例:

public class Client {
    public static void main(String[] args) {
        // 创建华为工厂
        HuaweiFactory huaweiFactory = new HuaweiElectronicsFactory();
        Television huaweiTelevision = huaweiFactory.createTelevision();
        huaweiTelevision.play();
        Audio huaweiAudio = huaweiFactory.createAudio();
        huaweiAudio.play();
        Computer huaweiComputer = huaweiFactory.createComputer();
        huaweiComputer.play();

        // 创建小米工厂
        XiaomiFactory xiaomiFactory = new XiaomiElectronicsFactory();
        Television xiaomiTelevision = xiaomiFactory.createTelevision();
        xiaomiTelevision.play();
        Audio xiaomiAudio = xiaomiFactory.createAudio();
        xiaomiAudio.play();
        Computer xiaomiComputer = xiaomiFactory.createComputer();
        xiaomiComputer.play();

        // 创建苹果工厂
        AppleFactory appleFactory = new AppleElectronicsFactory();
        Television appleTelevision = appleFactory.createTelevision();
        appleTelevision.play();
        Audio appleAudio = appleFactory.createAudio();
        appleAudio.play();
        Computer appleComputer = appleFactory.createComputer();
        appleComputer.play();
    }
}

运行上述代码,将会输出以下结果:

Playing on Huawei Television
Playing on Huawei Audio
Playing on Huawei Computer
Playing on Xiaomi Television
Playing on Xiaomi Audio
Playing on Xiaomi Computer
Playing on Apple Television
Playing on Apple Audio
Playing on Apple Computer

可以看到,不同品牌的工厂生产出的产品是不同的,但是它们都符合对应的产品接口。通过抽象工厂模式,我们可以方便地扩展电子设备的种类,同时保持客户端与产品类的解耦。