Design pattern - abstract factory pattern

Keywords: Java Design Pattern

preface

The same kind is called the same level, that is, the factory method mode only considers the production of products of the same level, but in real life, many factories are comprehensive factories that can produce multi-level (type) products. For example, mobile phone factories produce both mobile phones and computers. The abstract factory mode will consider the production of multi-level products of the same product family. (the image of the same product family can be understood as the same brand.)

definition

It is a pattern structure that provides an interface for the access class to create a group of related or interdependent objects, and the access class can obtain different levels of products of the same family without specifying the specific class of the product.

Role and class diagram of pattern

role

The roles of the abstract factory are as follows:

  • Abstract Factory: it provides an interface for creating products. It contains multiple methods for creating products, and can create multiple products of different levels.
  • Concrete Factory: it mainly implements multiple abstract methods in the abstract factory to complete the creation of specific products.
  • Abstract Product: it defines the Product specification and describes the main features and functions of the Product. The abstract factory pattern has multiple Abstract products.
  • Concrete product: it implements the interface defined by the abstract product role and is created by the concrete factory. It has a many-to-one relationship with the concrete factory.

Class diagram

The specific codes are as follows:

/**
 * @author hao
 * Abstract product
 */
public interface Phone {
    void getName();
    void call();
}
/**
 * Specific products 1
 */
public class HuaweiPhone implements Phone {
    @Override
    public void getName() {
        System.out.println("Huawei mobile phone...");
    }

    @Override
    public void call() {
        System.out.println("Huawei mobile phone calls...");
    }
}
/**
 * Specific products 2
 */
public class XiaomiPhone implements Phone {
    @Override
    public void getName() {
        System.out.println("Mi phones...");
    }

    @Override
    public void call() {
        System.out.println("Millet phone call...");
    }
}
/**
 * Abstract product 2
 */
public interface Computer {
    void getName();
    void playGame();
}
/**
 * Specific products 2 (1)
 */
public class HuaweiComputer implements Computer {
    @Override
    public void getName() {
        System.out.println("Huawei computer...");
    }

    @Override
    public void playGame() {
        System.out.println("Huawei computers are playing games...");
    }
}
/**
 * Specific products 2 (2)
 */
public class XiaomiComputer implements Computer {
    @Override
    public void getName() {
        System.out.println("Xiaomi computer...");
    }

    @Override
    public void playGame() {
        System.out.println("Xiaomi computer is playing games...");
    }
}

/**
 * Abstract factory
 */
public interface ElectricFactory {
    Computer createCar();
    Phone createPhone();
}
/**
 * Specific factory 1
 */
public class HuaweiElectricFactory implements ElectricFactory {
    @Override
    public Computer createCar() {
        return new HuaweiComputer();
    }

    @Override
    public Phone createPhone() {
        return new HuaweiPhone();
    }
}
/**
 * Specific plant 2
 */
public class XiaomiElectricFactory implements ElectricFactory {
    @Override
    public Computer createCar() {
        return new XiaomiComputer();
    }

    @Override
    public Phone createPhone() {
        return new XiaomiPhone();
    }
}

The test code is as follows:

public class MainTest {
    public static void main(String[] args){
        ElectricFactory xiaomiF = new XiaomiElectricFactory();
        Computer xiaomiFComputer = xiaomiF.createComputer();
        Phone xiaomiPhone = xiaomiF.createPhone();
        xiaomiFComputer.getName();
        xiaomiFComputer.playGame();
        xiaomiPhone.getName();
        xiaomiPhone.call();


        ElectricFactory huaweiF = new HuaweiElectricFactory();
        Computer huaweiFCar = huaweiF.createComputer();
        Phone huweiFPhone = huaweiF.createPhone();
        huaweiFCar.getName();
        huaweiFCar.playGame();
        huweiFPhone.getName();
        huweiFPhone.call();

    }
}

result:

Xiaomi computer
Xiaomi computer is playing games
Xiaomi mobile phone
Millet phone call
Huawei computer
Huawei computer is playing games
Huawei mobile
Huawei mobile phone call

Analysis of advantages and disadvantages

advantage:

When multiple objects in a product family are designed to work together, it can ensure that the client always uses only the objects in the same product family.

Disadvantages:

When a new product needs to be added to the product family, all factory classes need to be modified. (advantages and disadvantages come from Baidu)

Posted by philvia on Wed, 06 Oct 2021 08:11:18 -0700