1. Intention
Define an interface for creating objects and let subclasses decide which class to instantiate. Factory Method uses a class to defer instantiation to its subclasses [GOF95]
2. Factory pattern structure diagram
3. Main characters in factory mode
Abstract product (Product) role: The parent class or interface common to specific product objects
Concrete Product role: Implements the interface defined by the abstract product role, and each object created by the factory method pattern is an instance of a specific product object
Abstract Factory (Creator) role: Any factory class that creates objects in the pattern must implement this interface. It declares a factory method that returns an object of type Product.
Creator can also define a default implementation of the factory method, which returns a default ConcreteProduct object
Concrete Creator (Concrete Creator) role: implements the abstract factory interface. The concrete factory role is related to application logic and is directly called by the application to create product objects.
4. Advantages and Disadvantages of Factory Model
The advantages of factory mode:
The factory method pattern allows the system to introduce new products without modifying the factory role.
Disadvantages of Factory Pattern:
Clients may have to create a Creator subclass just to create a specific ConcreteProduct object
5. Factory mode applicable scenarios
1. When a class does not know the class of the object it must create
2. When a class wants its subclass to specify the object it creates
3. When a class delegates the responsibility of creating objects to one of multiple helper subclasses, and you want to localize the information about which helper subclass is the delegate
6. Factory mode and other modes
Abstract factory mode: Abstract Factory mode is often implemented using factory methods
Template Method pattern: Factory methods are usually called in Template Methods
7. Factory mode PHP example
<?php /** * 抽象工厂角色 */ interface Creator { public function factoryMethod(); } /** * 具体工厂角色A */ class ConcreteCreatorA implements Creator { /** * 工厂方法 返回具体 产品A * @return ConcreteProductA */ public function factoryMethod() { return new ConcreteProductA(); } } /** * 具体工厂角色B */ class ConcreteCreatorB implements Creator { /** * 工厂方法 返回具体 产品B * @return ConcreteProductB */ public function factoryMethod() { return new ConcreteProductB(); } } /** * 抽象产品角色 */ interface Product { public function operation(); } /** * 具体产品角色A */ class ConcreteProductA implements Product { /** * 接口方法实现 输出特定字符串 */ public function operation() { echo 'ConcreteProductA <br />'; } } /** * 具体产品角色B */ class ConcreteProductB implements Product { /** * 接口方法实现 输出特定字符串 */ public function operation() { echo 'ConcreteProductB <br />'; } } class Client { /** * Main program. */ public static function main() { $creatorA = new ConcreteCreatorA(); $productA = $creatorA->factoryMethod(); $productA->operation(); $creatorB = new ConcreteCreatorB(); $productB = $creatorB->factoryMethod(); $productB->operation(); } } Client::main(); ?>
8. Distinguish between factory method pattern and simple factory pattern
The structural difference between the factory method pattern and the simple factory pattern is not very obvious. The core of the factory method class is an abstract factory class, while the simple factory pattern puts the core on a concrete class.
The reason why the factory method pattern has an alias called polymorphic factory pattern is because specific factory classes have a common interface or a common abstract parent class.
When the system expands and needs to add a new product object, it only needs to add a specific object and a specific factory object. The original factory object does not need to be modified in any way, and there is no need to modify the client, which is in line with the "open-closed" concept. in principle. The simple factory model has to modify the factory method after adding a new product object, which has poor scalability.
The factory method pattern can evolve into a simple factory pattern after degeneration.
The above is the code to implement the factory mode using PHP. There are also some conceptual distinctions about the factory mode. I hope it will be helpful to everyone's learning.