Abstract factory pattern (java)

Table of contents

structure

Axis understanding

Cafe case

Code

abstract factory class

Concrete factory class

abstract product class

specific product category

test class

Advantages and disadvantages


structure

The main roles of the abstract factory pattern are as follows:

  • Abstract Factory (Abstract Factory) : Provides an interface for creating products, which 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 (Product) : defines the specification of the product, describes the main features and functions of the product, and the abstract factory pattern has multiple abstract products.

  • Concrete Product : implements the interface defined by the abstract product role, and is created by a specific factory. It has a many-to-one relationship with the specific factory.

Axis understanding

 PS: The product level corresponds to the abstract product class, and the product family corresponds to the abstract factory class. The same specific factory can manufacture products of different grades, and the same product can be manufactured by different factories. (The abstract factory class is willing to be an interface class)

Cafe case

The current coffee shop business not only produces coffee but also produces desserts, such as tiramisu, matcha mousse, etc. If it follows the factory method model, it is necessary to define tiramisu, matcha mousse, tiramisu factory, Matcha mousse factories and dessert factories are prone to explosions. Among them, latte coffee and American coffee are one product grade, both of which are coffee; tiramisu and matcha mousse are also one product grade; latte coffee and tiramisu are the same product family (that is, both belong to Italian style), Americano and matcha mousse are the same product family (that is, both belong to the American flavor). So this case can be implemented using the abstract factory pattern. The class diagram is as follows:

Code

abstract factory class

package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 抽象点心工厂类
 * @date: 2023/7/18 20:07
 */
public interface DessertFactory {

    Coffee createCoffee();
    Dessert createDessert();

}

Concrete factory class

package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 意大利甜味工厂
 * @date: 2023/7/18 20:26
 */
public class ItalyDessertFactory implements DessertFactory{

    @Override
    public Coffee createCoffee() {
        return new LatteCoffee();
    }

    @Override
    public Dessert createDessert() {
        return new Tiramisu();
    }

}
package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 美式工厂
 * @date: 2023/7/18 20:20
 */
public class AmericanDessertFactory implements DessertFactory{

    @Override
    public Coffee createCoffee() {
        // 美式工厂创建美式咖啡
        return new AmericanCoffee();
    }

    @Override
    public Dessert createDessert() {
        // 美式工厂创建抹茶
        return new MatchaMousse();
    }


}

abstract product class

abstract coffee class

package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 咖啡类
 * @date: 2023/7/18 14:43
 */
public abstract class Coffee {

    public abstract  void  getName();

}

Abstract dim sum class 

package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 抽象甜品类
 * @date: 2023/7/18 20:13
 */
public abstract class Dessert {

    public abstract void show();

}

specific product category

coffee

package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 美式咖啡
 * @date: 2023/7/18 14:47
 */
public class AmericanCoffee extends Coffee{

    public  void getName(){
        System.out.println("美式咖啡");
    }

}
package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 拿铁咖啡
 * @date: 2023/7/18 14:59
 */
public class LatteCoffee extends Coffee {

    public  void  getName(){
        System.out.println("拿铁咖啡");
    }

}

dessert

package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 抹茶类
 * @date: 2023/7/18 20:22
 */
public class MatchaMousse extends Dessert {

    @Override
    public void show() {
        System.out.println("抹茶慕斯");
    }

}
package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 提拉米苏(具体甜品类)
 * @date: 2023/7/18 20:14
 */
public class Tiramisu extends Dessert{

    @Override
    public void show() {
        System.out.println("提拉米苏");
    }

}

test class

package abstract_factory;

/**
 * @author: ZQH
 * @project: Design Pattern
 * @description 测试类
 * @date: 2023/7/18 20:05
 */
public class Client {
    public static void main(String[] args) {

        // 创建意大利工厂对象
        ItalyDessertFactory factory1 = new ItalyDessertFactory();

        // 工厂制作点心
        Coffee coffee1 =  factory1.createCoffee();
        Dessert dessert1 =  factory1.createDessert();
        // 展示
        coffee1.getName();
        dessert1.show();

        System.out.println("_______________________");

        // 创建美式工厂对象
        AmericanDessertFactory factory2 = new AmericanDessertFactory ();

        // 工厂制作点心
        Coffee coffee2 =  factory2.createCoffee();
        Dessert dessert2 =  factory2.createDessert();

        // 展示
        coffee2.getName();
        dessert2.show();

    }

}

Advantages and disadvantages

advantage

When multiple objects in a product family are designed to work together, it guarantees that clients always use only objects from the same product family.

shortcoming

When a new product needs to be added to the product family, all factory classes need to be modified.

Guess you like

Origin blog.csdn.net/qq_51179608/article/details/131795974