“This is the 28th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Abstract Factory pattern

This type of design pattern belongs to the creation pattern. The disadvantage of the factory method pattern mentioned last time is that a specific factory can only produce one kind of product, which can be solved by the abstract factory pattern.

The main difference between the abstract factory pattern and the factory method pattern is that each factory in an abstract factory can create multiple kinds of products. The factory method can only create one class per factory

The abstract factory pattern provides an interface for creating a series of related or interdependent objects, without specifying their concrete classes, and combining some related concrete classes into a “concrete class family” that is collectively produced by the same factory.

implementation

Abstract Factory class

Declare a set of methods for creating a family of products, one object for each method, and declare multiple factory methods in an abstract factory for creating different types of objects. (Can return different types of objects)

abstract class Factory{
    public abstract Product ManufacturePhone(a);
    public abstract Product ManufactureComputer(a);
}
Copy the code

Abstract product family

Provide a set of business methods that all classes have.

abstract class Product{
    public abstract void Show(a);
}
Copy the code

Abstract product class

Implement the business methods defined in the abstract interface (again abstract)

abstract class Phone extends Product {
    @Override
    public abstract void play(a);
}
abstract class Computer extends Product {
    @Override
    public abstract void play(a);
}
Copy the code

Specific product category

This role inherits the abstract class and is primarily used to implement abstract methods declared in the abstract class

public class APhone extends Phone{
    @Override
    public void play(a) {}}public class AComputer extends Computer {
    @Override
    public void play(a) {}}public class BPhone extends Phone {
    @Override
    public void play(a) {}}public class BComputer extends Computer{
    @Override
    public void play(a) {}}Copy the code

Specific Factory

Create an object

public class FactoryA extends Factory {
    @Override
    public Product ManufacturePhone(a) {
        return new APhone();
    }
​
    @Override
    public Product ManufactureComputer(a) {
        return newAComputer(); }}public class FactoryB extends Factory {
    @Override
    public Product ManufacturePhone(a) {
        return new BPhone();
    }
​
    @Override
    public Product ManufactureComputer(a) {
        return newBComputer(); }}Copy the code

In the abstract factory pattern, each concrete factory provides multiple factory methods for creating concrete objects of different types, which constitute a family.

It is very convenient to add a new family without modifying the existing system, in line with the “open closed principle”.