“This is the 14th day of my participation in the First Challenge 2022.

This article focuses on the Abstract Factory Pattern, which is defined as “providing an interface for creating a series of related or interdependent objects without specifying their concrete classes.” .

1. Mode introduction

The Factory Design Pattern is a very common Design Pattern for creating objects, and has best practices in most open source frameworks, perhaps best known as Spring’s BeanFactory.

The factory pattern is a creation pattern. In fact, it can be implemented in three different ways: simple factory pattern, factory method pattern, and abstract factory pattern.

This article focuses on the Abstract Factory Pattern, which is defined as “providing an interface for creating a series of related or interdependent objects without specifying their concrete classes.” .

Abstract Factory pattern is an updated version of the factory method pattern, in which the abstract factory interface has several methods that need to be implemented by concrete factory classes. In other words, a factory class in the abstract factory pattern can create multiple objects that do not inherit or implement interfaces.

2. Application scenarios

The abstract factory pattern is suitable for situations where a class of objects, or a class of objects that have no concrete relationships, can be used if they have the same constraints.

This paragraph is taken from 9.3.3 Usage scenarios of abstract Factory patterns in Zen of Design Patterns (2nd edition)

3. The class diagram

There are four roles in the abstract factory method pattern:

  • Abstract Product (IProduct) : An abstract interface that describes all objects created by the factory
  • Concrete Products: Concrete instances of factory creation that implement abstract Product interfaces
  • Abstract Factory: Describes how a Factory creates a concrete product. Any Factory class used to create a concrete product must implement this interface
  • Concrete Factory: A Concrete Factory class that creates Concrete products and implements an abstract Factory that describes the process of creating Concrete products of a specified type

These four roles are the same as the factory method pattern, but the difference between the abstract factory pattern and the factory method pattern is that in the abstract factory pattern, a factory can create multiple types of objects, whereas in the factory method pattern, a factory can create only one type of objects.

4. Implementation method

The following shows how the abstract factory pattern is implemented based on a simple scenario.

Citing the example scenario in introducing the factory method mode, there are two major mobile phone brands IPhone and Huawei and the mobile phone factories used to create these two mobile phone brands: IPhoneFactory and HuaweiFactory.

Suppose we now have another requirement to create headphones. Let’s define the headphone jack, which has a method for playing music:

public interface HeadSet {
    void playMusic(a);
}
Copy the code

Then create IPhone and HuaWei corresponding earphone accessories — AirPods and FreeBuds respectively to realize HeadSet HeadSet interface.

public class AirPods implements HeadSet {
    @Override
    public void playMusic(a) {
        System.out.println("AirPods... Seems to work well?"); }}public class FreeBuds implements HeadSet {
    @Override
    public void playMusic(a) {
        System.out.println("Huawei Bluetooth headset... A little uglier?"); }}Copy the code

We then define a new abstract factory that has two methods, one for creating mobile phones and one for creating headphones.

public interface NewPhoneFactory {

    /** * Get the phone object interface method *@return Phone
     */
    Phone getPhone(a);

    /** * Get the earphone interface method *@return* /
    HeadSet getHeadSet(a);
}
Copy the code

The specific factory classes of IPhone and Huawei are relatively clear, as follows:

public class NewIPhoneFactory implements NewPhoneFactory {
    @Override
    public Phone getPhone(a) {
        return new IPhone();
    }

    @Override
    public HeadSet getHeadSet(a) {
        return newAirPods(); }}public class NewHuaWeiFactory implements NewPhoneFactory {
    @Override
    public Phone getPhone(a) {
        return new HuaWei();
    }

    @Override
    public HeadSet getHeadSet(a) {
        return newFreeBuds(); }}Copy the code

Finally, we write some code to test the abstract factory.

public static void main(String[] args) {
    NewIPhoneFactory iPhoneFactory = new NewIPhoneFactory();
    // Get the phone object
    Phone iPhone = iPhoneFactory.getPhone();
    iPhone.takePhotos();
    // Get the earphone object
    HeadSet airPods = iPhoneFactory.getHeadSet();
    airPods.playMusic();
}

/ / output
// IPhone... The devil pre -
// AirPods... Seems to work pretty well?
Copy the code

5. The advantages and disadvantages

Finally, as usual, the advantages and disadvantages of the abstract factory pattern.

Advantages of the Abstract factory pattern:

  • A concrete factory class can create multiple type objects in a product family
  • Good encapsulation decouples the client from the logic that creates the concrete product

Disadvantages of the Abstract factory pattern:

  • Poor expansion, in violation of the open and closed principle. If you add another method to create a charger in the example above, you will need to add this method to all abstraction factories.

6. Summary

This paper describes the definition, application scenarios, class diagrams, implementation methods and advantages and disadvantages of the abstract factory pattern.

7. Reference materials

  • Zen of Design Patterns (2nd Edition)

Finally, this article is included in the Personal Speaker Knowledge Base: Back-end technology as I understand it, welcome to visit.