PHP简单工厂模式、工厂方法模式和抽象工厂模式比较
PHP工厂模式概念:工厂模式是一种类,它具有为您创建对象的某些方法。您可以使用工厂类创建对象,而不直接使用 new。这样,如果您想要更改所创建的对象类型,只需更改该工厂即可。使用该工厂的所有代码会自动更改。
根据抽象程度不同,PHP工厂模式分为:简单工厂模式、工厂方法模式和抽象工厂模式
简单工厂模式:
/******代码在原文还是git上都有osgit地址 *******/
/** *简单工厂模式与工厂方法模式比较。 *简单工厂又叫静态工厂方法模式,这样理解可以确定,简单工厂模式是通过一个静态方法创建对象的。 */interface people {function jiehun();}class man implements people{function jiehun() { echo '送玫瑰,送戒指!<br>';}} class women implements people {function jiehun() { echo '穿婚纱!<br>';}} class SimpleFactoty {// 简单工厂里的静态方法static function createMan() {return new man;}static function createWomen() {return new women;}} $man = SimpleFactoty::createMan();$man->jiehun();$man = SimpleFactoty::createWomen();$man->jiehun();
工厂方法模式:
<?php/* *工厂方法模式: *定义一个创建对象的接口,让子类决定哪个类实例化。 他可以解决简单工厂模式中的封闭开放原则问题。<www.phpddt.com整理> */interface people {function jiehun();}class man implements people{function jiehun() { echo '送玫瑰,送戒指!<br>';}} class women implements people {function jiehun() { echo '穿婚纱!<br>';}} 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();
抽象工厂模式:
<?php/*抽象工厂:提供一个创建一系列相关或相互依赖对象的接口。 注意:这里和工厂方法的区别是:一系列,而工厂方法则是一个。那么,我们是否就可以想到在接口create里再增加创建“一系列”对象的方法呢?*/interface people {function jiehun();}class Oman implements people{function jiehun() { echo '美女,我送你玫瑰和戒指!<br>';}}class Iman implements people{function jiehun() { echo '我偷偷喜欢你<br>';}} class Owomen implements people {function jiehun() { echo '我要穿婚纱!<br>';}} class Iwomen implements people {function jiehun() { echo '我好害羞哦!!<br>';}} 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();
区别:
简单工厂模式:用来生产同一等级结构中的任意产品。对与增加新的产品,无能为力
工厂模式 :用来生产同一等级结构中的固定产品。(支持增加任意产品)
抽象工厂 :用来生产不同产品族的全部产品。(对于增加新的产品,无能为力;支持增加产品族)
以上三种工厂 方法在等级结构和产品族这两个方向上的支持程度不同。所以要根据情况考虑应该使用哪种方法
适用范围:
简单工厂模式:
工厂类负责创建的对象较少,客户只知道传入工厂类的参数,对于如何创建对象不关心。
工厂方法模式:
当一个类不知道它所必须创建对象的类或一个类希望由子类来指定它所创建的对象时,当类将创建对象的职责委托给多个帮助子类中得某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候,可以使用工厂方法模式。
抽象工厂模式:
一个系统不应当依赖于产品类实例何如被创建,组合和表达的细节,这对于所有形态的工厂模式都是重要的。这个系统有多于一个的产品族,而系统只消费其 中某一产品族。同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。系统提供一个产品类的库,所有的产品以同样的接口出现,从 而使客户端不依赖于实现。
无论是简单工厂模式、工厂模式还是抽象工厂模式,它们本质上都是将不变的部分提取出来,将可变的部分留作接口,以达到最大程度上的复用。究竟用哪种设计模式更适合,这要根据具体的业务需求来决定。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
