Design pattern --- abstract factory pattern

Posted by Jaguar on Wed, 22 Dec 2021 18:39:53 +0100

Abstract factory pattern

To learn the abstract factory pattern, we must first understand the factory pattern
In the factory method mode, only products of the same grade are considered, but in real life, many factories are comprehensive factories, It can produce multi-level (variety) products, such as raising animals and plants on the farm, producing televisions and washing machines or air conditioners in electrical factories, and both software and biology majors in universities. The abstract factory model will consider the production of multi-level products, and call a group of products at different levels produced by the same specific factory as a product family

Definition and characteristics of abstract factory pattern

definition
It is a pattern structure that provides an interface for an 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
characteristic

  • The multi-level products associated in the product family can be managed together within the class, instead of introducing multiple new classes for management.
  • When a product family is required, the abstract factory can ensure that the client always uses only the product group of the same product.
  • Abstract factory enhances the scalability of the program. When adding a new product family, there is no need to modify the original code to meet the opening and closing principle.

shortcoming
When a new product needs to be added to the product family, all factory classes need to be modified. It increases the abstraction and understanding difficulty of the system

Structure and Implementation

Like the factory method pattern, the abstract factory pattern is also composed of four elements: abstract factory, concrete factory, abstract product and concrete product. However, the number of methods in the abstract factory is different, so is the number of abstract products
Pattern structure
The main role of the abstract factory pattern

  1. Abstract Factory: it provides an interface for creating products. It contains multiple methods for creating products newProduct(), which can create multiple products of different levels.
  2. Concrete Factory: it mainly implements multiple abstract methods in the abstract factory to complete the creation of specific products.
  3. 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.
  4. 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.
    Structure diagram

    The example realizes the production of mobile phones and routers, as shown in the figure:

    Implementation code

Interface

//Mobile product interface
public interface IphoneProduct {
    void start();
    void stop();
    void collup();
    void sendMS();
}
//Router product interface
public interface IRouter {
    void start();
    void stop();
    void openWIFI();
    void setting();
}
//Abstract product factory interface
public interface IFactory {
//Mobile phone factory
    IphoneProduct iphoneproduct();
//Generate router factory
    IRouter irouterproduct();

}

factory

//Huawei factory
public class huaweiFactory implements IFactory{
    @Override
    public IphoneProduct iphoneproduct() {
        return new huaweiPhone();
    }

    @Override
    public IRouter irouterproduct() {
        return new huaweiRouter();
    }
}

//Millet factory
public class xiaomiFactory implements IFactory{
    @Override
    public IphoneProduct iphoneproduct() {//Mobile phone module
        return new xiaomiPhone();
    }

    @Override
    public IRouter irouterproduct() {//Router module
        return new xiaomiRouter();
    }
}

Product production

//Xiaomi mobile phone production
public class xiaomiPhone implements IphoneProduct{
    @Override
    public void start() {
        System.out.println("Production of millet boot module");
    }

    @Override
    public void stop() {
        System.out.println("Production millet shutdown module");
    }

    @Override
    public void collup() {
        System.out.println("Production of Xiaomi telephone module");
    }

    @Override
    public void sendMS() {
        System.out.println("Production of Xiaomi SMS module");
    }
}

//Millet router production
public class xiaomiRouter implements IRouter {
    @Override
    public void start() {
        System.out.println("Production of millet router boot module");
    }

    @Override
    public void stop() {
        System.out.println("Production of Xiaomi router shutdown module");
    }

    @Override
    public void openWIFI() {
        System.out.println("Production of millet router opening module");
    }

    @Override
    public void setting() {
        System.out.println("Production millet router setting module");
    }
}

//Huawei mobile phone production
public class huaweiPhone implements IphoneProduct{
    @Override
    public void start() {
        System.out.println("Production of Huawei boot module");
    }

    @Override
    public void stop() {
        System.out.println("Production Huawei shutdown module");
    }

    @Override
    public void collup() {
        System.out.println("Production of Huawei call module");
    }

    @Override
    public void sendMS() {
        System.out.println("Production of Huawei SMS module");
    }
}
//Huawei router production
public class huaweiRouter implements IRouter{
    @Override
    public void start() {
        System.out.println("Production of Huawei router boot module");
    }

    @Override
    public void stop() {
        System.out.println("Production of Huawei router shutdown module");
    }

    @Override
    public void openWIFI() {
        System.out.println("Production Huawei router opening module");
    }

    @Override
    public void setting() {
        System.out.println("Production Huawei router setup module");
    }
}

Test class

public class Test {
    public static void main(String[] args) {
        System.out.println("--------Millet factory--------");
        xiaomiFactory xiaomiFactory=new xiaomiFactory();//Millet factory
        IphoneProduct iphoneproduct = xiaomiFactory.iphoneproduct();//Xiaomi factory produces mobile phones
        IRouter irouterproduct = xiaomiFactory.irouterproduct();//Xiaomi factory produces routers
        iphoneproduct.start();//Production boot module
        iphoneproduct.stop();//Production shutdown module
        iphoneproduct.collup();//Production call module
        iphoneproduct.sendMS();//Production SMS module
        irouterproduct.start();//Production router boot module
        irouterproduct.stop();//Production router shutdown module
        irouterproduct.openWIFI();//Production open router module
        irouterproduct.setting();//Production router setup module
        System.out.println("--------Huawei factory--------");
        huaweiFactory huaweiFactory = new huaweiFactory();//Millet factory
        IphoneProduct iphoneproduct1 = huaweiFactory.iphoneproduct();//Huawei factory produces mobile phones
        IRouter irouterproduct1 = huaweiFactory.irouterproduct();//Huawei factory produces routers
        iphoneproduct1.start();//Production boot module
        iphoneproduct1.stop();//Production shutdown module
        iphoneproduct1.collup();//Production call module
        iphoneproduct1.sendMS();//Production SMS module
        irouterproduct1.start();//Production router boot module
        irouterproduct1.stop();//Production router shutdown module
        irouterproduct1.openWIFI();//Production open router module
        irouterproduct1.setting();//Production router setup module
    }
}

Operation results

Topics: Java Design Pattern