Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Factory pattern in php Design pattern

Shulou Source: shulou.com Published: 2022-06-03 05:57:15 10月09日 Update

Editor to share with you how to use the factory pattern in the php design pattern. I hope you will get something after reading this article. Let's discuss it together.

Factory mode: it is up to the factory class to decide which instance of the production class to create based on parameters.

Factory class: a method class dedicated to creating other objects. That is, allocate according to demand, pass in parameters for selection, and return specific classes

Function: encapsulation of object creation, simplifying the operation of creating objects, that is, calling a method of the factory class to get the desired class

Add:

1. Main roles: abstract product (Product), concrete product (Concrete Product), abstract factory role (Creator)

two。 Advantages and disadvantages

Advantages: the factory method model allows the system to introduce heart products without modifying the factory role

Cons: customers may have to create a Creator subclass just to create a specific Concrete Product object

3. Applicability

When a class does not know the objects it must create

When a class wants its subclasses to determine the objects it creates

When a class delegates the responsibility of creating an object to one of several help subclasses, and you want you to localize the information about which help subclass is a proxy

The copy code is as follows:

?

After reading this article, I believe you have a certain understanding of "how to use the factory pattern in the php design pattern". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Factory object pattern subclass method product time role design pattern design code parameter article help distribution on demand advantages advantages advantages and disadvantages functions information multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB Xiaomi vpn Docker