Home > Article > Backend Development > A brief discussion on the simple factory pattern in PHP
This article takes you through the simple factory pattern in PHP design patterns. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Let’s start with a simple factory. Whether it is an interview or being interviewed by others, when asked about design patterns, most people will mention the factory pattern. There is no doubt that several factory-related patterns are the most famous and widely used among design patterns. GoF design patterns are also creational patterns.
However, those who can explain the three modes of Simple Factory, Factory Pattern, and Abstract Factory Pattern can really impress the interviewer. . There is a premise here that you can really explain it clearly. Most people, including me, cannot explain it clearly before studying design patterns in depth. Whether it’s me going for an interview or interviewing someone else. When I interview others, I can only give a rough outline. When I went to the interview, I just gave a general outline. After experiencing many setbacks, I came up with the idea of researching design patterns in depth, so this series of articles was produced. Starting from this simple factory, let's study design patterns in depth again.
Simple factory, also known as static factory, does not belong to the GoF 23 design patterns. But it can be said that among all design patterns, it is probably the easiest for everyone to understand, and you may have already used this design pattern countless times in your code. Let’s start with the simplest code segment.
// Factory class Factory { public static function createProduct(string $type) : Product { $product = null; switch ($type) { case 'A': $product = new ProductA(); break; case 'B': $product = new ProductB(); break; } return $product; } }
Yes, the core point is the simple switch code in the middle. We fixed the return value type as the implementation of the Product interface.
In this code, new features of PHP are used, Parameter type and Return value type
Product interface And product implementation
// Products interface Product { public function show(); } class ProductA implements Product { public function show() { echo 'Show ProductA'; } } class ProductB implements Product { public function show() { echo 'Show ProductB'; } }
Finally, the use of the client is very simple
// Client $productA = Factory::createProduct('A'); $productB = Factory::createProduct('B'); $productA->show(); $productB->show();
As can be seen from the above code, in fact, here is a factory class based on the string we pass in or other definitions you make. identifier to return the corresponding product (Product object).
A more visual metaphor: I am a wholesaler (Client, business side) selling mobile phones. I need a batch of mobile phones (Product), so I go to Foxconn ( Factory) to help me produce. I placed an order ($type variable) and specified the model, and then Foxconn gave me the phone of the corresponding model, and then I continued my work. It was really pleasant to cooperate with Foxconn.
The more standardized way of writing here may be that all products will implement a unified interface, and then the client only knows how to call the interface methods uniformly. If it is not standardized, you can not use interfaces and return various objects, similar to the facade mode for unified facade management.
Source code address: Simple factory basic class diagram implementation
https://github.com/zhangyue0503/designpatterns-php/blob/master /01.simple-factory/source/simple-factory.php
Scenario: SMS sending function module. Now we use the SMS services of three merchants, namely Alibaba Cloud, Diexin, and Jiguang. Different SMS senders may be used in different businesses. Using Simple Factory can easily fulfill this requirement.
Class diagram:
Code:
<?php interface Message { public function send(string $msg); } class AliYunMessage implements Message{ public function send(string $msg){ // 调用接口,发送短信 // xxxxx return '阿里云短信(原阿里大鱼)发送成功!短信内容:' . $msg; } } class BaiduYunMessage implements Message{ public function send(string $msg){ // 调用接口,发送短信 // xxxxx return '百度SMS短信发送成功!短信内容:' . $msg; } } class JiguangMessage implements Message{ public function send(string $msg){ // 调用接口,发送短信 // xxxxx return '极光短信发送成功!短信内容:' . $msg; } } Class MessageFactory { public static function createFactory($type){ switch($type){ case 'Ali': return new AliYunMessage(); case 'BD': return new BaiduYunMessage(); case 'JG': return new JiguangMessage(); default: return null; } } } // 当前业务需要使用极光 $message = MessageFactory::createMessage('Ali'); echo $message->send('您有新的短消息,请查收');
Source code address : Simple factory instance-SMS sending factory
https://github.com/zhangyue0503/designpatterns-php/blob/master/01.simple-factory/source/simple-factory-message.php
illustrate
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of A brief discussion on the simple factory pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!