Recently I was reading about the Factory Pattern and the Abstract Factory Pattern. I read this part of the in-depth PHP book twice, but I was still confused, so I asked Du Niang. I think there is a blog post that said this very well. Makes sense:
Factory methods
The factory method provides a factory class for each product. Different product instances are created through different factory instances.
In the same hierarchical structure, any product can be added.
Abstract Factory
Abstract factory responds to the concept of product family. For example, each automobile company may want to produce cars, trucks, and buses at the same time, so each factory must have a method for creating cars, trucks, and buses.
Born in response to the concept of product family, it is easy to add new product lines, but it is impossible to add new products.
See the blog post for details
In short, the factory method is to produce different products of the same level, while the abstract factory is to produce different product families, and the factory method is often used in abstract factories.
First, let’s introduce the factory method.
Suppose I have a personnel management project that needs to manage Appointment objects, and I am required to use BloggsCal or MegaCal format to communicate with them about appointments. Of course, as time goes by, the required formats will definitely increase. (In my opinion, the format here is equivalent to the products in factory mode)
At the interface level we can define two classes. One requires one to convert the Appointment object into a proprietary format and name it the AppEncoder class (product product). The other requires an administrator to obtain this encoder. We name it the CommsManager class (creator).
Next we get to the code:
<code><span>abstract</span><span><span>class</span><span>ApptEncoder</span>{</span><span>abstract</span><span><span>function</span><span>encode</span><span>()</span>;</span> } <span><span>class</span><span>BloggsApptEncoder</span><span>extends</span><span>ApptEncoder</span>{</span><span><span>function</span><span>encode</span><span>()</span>{</span><span>return</span><span>"Appointment data encode in BloggsCal format \n"</span>; } } <span>abstract</span><span><span>class</span><span>CommsManager</span>{</span><span>abstract</span><span><span>function</span><span>getHeadText</span><span>()</span>;</span><span>abstract</span><span><span>function</span><span>getApptEncoder</span><span>()</span>;</span><span>abstract</span><span><span>function</span><span>getFooterText</span><span>()</span>;</span> } <span><span>class</span><span>BloggsCommsManager</span><span>extends</span><span>CommsManager</span>{</span><span><span>function</span><span>getHeadText</span><span>()</span>{</span><span>return</span><span>"BloggsCal header\n"</span>; } <span><span>function</span><span>getApptEncoder</span><span>()</span>{</span><span>return</span><span>new</span> BloggsApptEncoder(); } <span><span>function</span><span>getFooterText</span><span>()</span>{</span><span>return</span><span>"BloggsCal footer \n"</span>; } }</code>
Here I only wrote a BloggsCal format, which is equivalent to making a product. If you add other formats such as MegaCal, you only need to inherit the ApptEncoder class and the CommsManager class. This is the factory. method.
But have you found that this pattern forms a special code duplication, and may lead to unnecessary instantiation, so you should consider it carefully before using the factory pattern
The following is an introduction to the abstract factory,
As mentioned above, the factory pattern is used to produce different products and the abstract factory is used to generate different product families. Let's continue with the previous example.
At this time, the project no longer has only one Appointment (appointment) function. It needs to add Ttd (to-do items) and Contact (contact) functions. This is to add a product family. I will write down the class diagram to save the money. I’ve written code and it’s clearer.
The above introduces the factory pattern and abstract factory of PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.