Comparison of PHP simple factory pattern, factory method pattern and abstract factory pattern

高洛峰
Release: 2023-03-03 15:12:01
Original
1316 people have browsed it

PHP Factory PatternConcept: Factory pattern is a class that has certain methods that create objects for you. You can use a factory class to create objects without using new directly. This way, if you want to change the type of object created, you only need to change the factory. All code using this factory is automatically changed.
According to different levels of abstraction, PHP factory patterns are divided into: simple factory pattern, factory method pattern and abstract factory pattern

Simple factory pattern:

/** *简单工厂模式与工厂方法模式比较。 *简单工厂又叫静态工厂方法模式,这样理解可以确定,简单工厂模式是通过一个静态方法创建对象的。 */ interface people { function jiehun(); } class man implements people{ function jiehun() { echo '送玫瑰,送戒指!
'; } } class women implements people { function jiehun() { echo '穿婚纱!
'; } } class SimpleFactoty { // 简单工厂里的静态方法 static function createMan() { return new man; } static function createWomen() { return new women; } } $man = SimpleFactoty::createMan(); $man->jiehun(); $man = SimpleFactoty::createWomen(); $man->jiehun();
Copy after login

Factory method pattern:

 */ interface people { function jiehun(); } class man implements people{ function jiehun() { echo '送玫瑰,送戒指!
'; } } class women implements people { function jiehun() { echo '穿婚纱!
'; } } interface createMan { // 注意了,这里是简单工厂本质区别所在,将对象的创建抽象成一个接口。 function create(); } class FactoryMan implements createMan{ function create() { return new man; } } class FactoryWomen implements createMan { function create() { return new women; } } class Client { // 简单工厂里的静态方法 function test() { $Factory = new FactoryMan; $man = $Factory->create(); $man->jiehun(); $Factory = new FactoryWomen; $man = $Factory->create(); $man->jiehun(); } } $f = new Client; $f->test();
Copy after login

Abstract factory pattern:

'; } } class Iman implements people{ function jiehun() { echo '我偷偷喜欢你
'; } } class Owomen implements people { function jiehun() { echo '我要穿婚纱!
'; } } class Iwomen implements people { function jiehun() { echo '我好害羞哦!!
'; } } interface createMan { // 注意了,这里是本质区别所在,将对象的创建抽象成一个接口。 function createOpen(); //分为 内敛的和外向的 function createIntro(); //内向 } class FactoryMan implements createMan{ function createOpen() { return new Oman; } function createIntro() { return new Iman; } } class FactoryWomen implements createMan { function createOpen() { return new Owomen; } function createIntro() { return new Iwomen; } } class Client { // 简单工厂里的静态方法 function test() { $Factory = new FactoryMan; $man = $Factory->createOpen(); $man->jiehun(); $man = $Factory->createIntro(); $man->jiehun(); $Factory = new FactoryWomen; $man = $Factory->createOpen(); $man->jiehun(); $man = $Factory->createIntro(); $man->jiehun(); } } $f = new Client; $f->test();
Copy after login

Difference:

Simple factory mode: used to produce any product in the same hierarchical structure. There is nothing you can do about adding new products

Factory mode: used to produce fixed products in the same hierarchical structure. (Supports adding any product)
Abstract factory: used to produce all products of different product families. (There is nothing you can do about adding new products; adding product families is supported)

The above three factory methods have different levels of support in the two directions of hierarchical structure and product family. So consider which method should be used according to the situation

Scope of application:

Simple factory pattern:

The factory class is responsible for creating fewer objects. The customer only knows the parameters passed into the factory class and does not care about how to create the object.

Factory Method Pattern:

When a class does not know the class of the object it must create or when a class wants a subclass to specify the object it creates, when the class delegates the responsibility of creating the object to multiple helper subclasses You can use the factory method pattern when you have one of these and you want to localize the information about which helper subclass is the delegate.

Abstract Factory Pattern:

A system should not depend on the details of how product class instances are created, composed and expressed. This is important for all forms of factory patterns. This system has more than one product family, and the system only consumes one of the product families. Products belonging to the same product family are used together, and this constraint must be reflected in the system design. The system provides a product class library, and all products appear with the same interface, so that the client does not depend on the implementation.

Whether it is a simple factory pattern, a factory pattern or an abstract factory pattern, they essentially extract the immutable parts and leave the variable parts as interfaces to achieve maximum reuse. Which design pattern is more suitable depends on specific business needs.

For more articles on the comparison of PHP simple factory pattern, factory method pattern and abstract factory pattern, please pay attention to the PHP Chinese website!

Related articles:

Detailed explanation of the specific code to implement the abstract factory pattern in Java

Abstract Factory Pattern of JAVA design pattern

PHP object-oriented development - Abstract Factory Pattern

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!