Home > Article > Backend Development > Learn PHP design patterns PHP implements abstract factory pattern_php skills
Abstract Factory pattern (Abstract Factory) is a common software design pattern. This pattern provides a unified creation interface for a product family. When a certain series of this product family is needed, a specific factory class can be created for this series of product families.
1. Intention
The Abstract Factory pattern provides an interface for creating system-related or interdependent objects without specifying their concrete classes [GOF95]
2. Abstract Factory Pattern Structure Diagram
3. Main roles in the abstract factory pattern
Abstract Factory role: It declares an interface for creating abstract product objects. Usually implemented as an interface or abstract class, all concrete factory classes must implement this interface or inherit this class.
Concrete Factory role: Realizes the operation of creating product objects. The client calls this role directly to create an instance of the product. This role contains the logic for selecting appropriate product objects. Usually implemented using concrete classes.
Abstract Product role: Declare the interface of a type of product. It is the parent class of objects created by the factory method pattern, or the interface they share.
Concrete Product role: Implement the interface defined by the abstract product role and define a product object that will be created by the corresponding concrete factory. It contains the business logic of the application.
4. Advantages and Disadvantages of the Abstract Factory Pattern
Advantages of the abstract factory pattern:
1. Separate specific classes
2. Make it easy to add or replace product families
3. Conducive to product consistency
The disadvantages of the abstract factory pattern: It is difficult to support new types of products. This is because the AbstractFactory interface determines the collection of products that can be created. Supporting new types of products requires extending the factory access interface, resulting in changes to the AbstractFactory class and all its subclasses.
The abstract factory supports the addition of new products in an oblique manner. It provides convenience for the addition of new product families, but cannot provide such convenience for the addition of new product hierarchical structures.
5. Applicable Scenarios of Abstract Factory Pattern
The abstract factory pattern should be used in the following situations:
1. A system should not depend on the details of how product class instances are created, combined, and expressed. This is important for all forms of factory patterns.
2. The products of this system have more than one product family, and the system only consumes products from one of the families.
3. Products belonging to the same product family are used together, and this constraint must be reflected in the system design.
4. The system provides a product class library, and all products appear with the same interface, so using the client does not depend on the implementation
6. Abstract Factory Pattern and Other Patterns
Singleton mode: A specific factory class can be designed as a singleton class. Since one factory is usually enough, specific factory subclasses are generally implemented as a Singleton.
Factory method pattern (factory method pattern): The method of creating products in an abstract factory is defined as a factory method.
Prototype mode (prototype mode): If there are multiple possible product series, a specific factory can also use the prototype mode, and the specific factory uses the product series
Each product prototype is instantiated and new products are created by copying its prototype.
7. Abstract Factory Pattern PHP Example
<?php /** * 抽象工厂 */ interface AbstractFactory { /** * 创建等级结构为A的产品的工厂方法 */ public function createProductA(); /** * 创建等级结构为B的产品的工厂方法 */ public function createProductB(); } /** * 具体工厂1 */ class ConcreteFactory1 implements AbstractFactory{ public function createProductA() { return new ProductA1(); } public function createProductB() { return new ProductB1(); } } /** * 具体工厂2 */ class ConcreteFactory2 implements AbstractFactory{ public function createProductA() { return new ProductA2(); } public function createProductB() { return new ProductB2(); } } /** * 抽象产品A */ interface AbstractProductA { /** * 取得产品名 */ public function getName(); } /** * 抽象产品B */ interface AbstractProductB { /** * 取得产品名 */ public function getName(); } /** * 具体产品A1 */ class ProductA1 implements AbstractProductA { private $_name; public function __construct() { $this->_name = 'product A1'; } public function getName() { return $this->_name; } } /** * 具体产品A2 */ class ProductA2 implements AbstractProductA { private $_name; public function __construct() { $this->_name = 'product A2'; } public function getName() { return $this->_name; } } /** * 具体产品B1 */ class ProductB1 implements AbstractProductB { private $_name; public function __construct() { $this->_name = 'product B1'; } public function getName() { return $this->_name; } } /** * 具体产品B2 */ class ProductB2 implements AbstractProductB { private $_name; public function __construct() { $this->_name = 'product B2'; } public function getName() { return $this->_name; } } /** * 客户端 */ class Client { /** * Main program. */ public static function main() { self::run(new ConcreteFactory1()); self::run(new ConcreteFactory2()); } /** * 调用工厂实例生成产品,输出产品名 * @param $factory AbstractFactory 工厂实例 */ public static function run(AbstractFactory $factory) { $productA = $factory->createProductA(); $productB = $factory->createProductB(); echo $productA->getName(), '<br />'; echo $productB->getName(), '<br />'; } } Client::main(); ?>
The above is the code that uses PHP to implement the abstract factory pattern. There are also some conceptual distinctions about the abstract factory pattern. I hope it will be helpful to everyone's learning.