Concepts provide an interface for creating a set of related or interdependent objects without specifying their concrete classes

Features Abstract Factory pattern (Abstact Factory) is a common software design pattern that provides a unified creation interface for a product family. When a family of products is needed, a concrete factory class can be created for that family of products.

The main role is the Abstract Factory role: It declares an interface to create Abstract product objects. It is usually implemented as an interface or abstract class that all concrete factory classes must implement or inherit from. Concrete Factory role: Implements the creation of product objects. The client calls this role directly to create an instance of the product. This role contains the logic to select the appropriate product object. It is usually implemented using concrete classes. Abstract Product role: An interface that declares a class of products. It is the parent class of the objects created by the factory method pattern, or the interface they share. Concrete Product role: Implements the interface defined by the abstract Product role, defining a Product object that will be created by the corresponding Concrete factory. It contains the business logic of the application. Advantages and disadvantages The advantages of the abstract factory pattern are: (1) It separates concrete classes; (2) it makes it easy to add or replace product families; (3) it facilitates product consistency

Disadvantages of the abstract factory pattern: It is difficult to support new kinds of products. This is because the AbstractFactory interface identifies the set of products that can be created. Supporting new classes of products requires the extension of the visiting factory interface, resulting in changes to the AbstractFactory class and all of its subclasses. Abstract factories support the addition of new products in a slanted way, providing convenience for the addition of new product families, but not for the addition of new product hierarchies. Applicability 1. It is important for all forms of factory patterns that a system should not depend on the details of how product class instances are created, composed, and expressed. 2. The products of the system have more than one product family, and the system only consumes the products of one of these family. 3. Products belonging to the same product family are used together, and this constraint must be reflected in the design of the system. 4. The system provides a library of product classes, and all products appear in the same interface, so that the client is implementation-independent

Abstract factory pattern instance

createCat(); $cat->Voice(); $dog = $AnimalFactory->createDog(); $dog->Voice(); } } Client::main(); ? >Copy the code