This article takes you through the factory method 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.
As mentioned last time, simple factory does not belong to the twenty-three design patterns of GoF. This time it comes to the real guy, the famous factory Method pattern comes to report!
Related recommendations: " A brief talk about the simple factory pattern in PHP"
Compared with the simple factory, the core point of the factory method pattern is to defer implementation to subclasses. How to understand it? We can use the simple factory from last time as the parent class, and then have a bunch of subclasses inherit it. The createProduct() method also becomes an abstract method in the parent class. Then all subclasses implement this method. There is no need to use switch to judge. The subclass can directly return an instantiated object.
GoF definition: Define an interface for creating objects and let subclasses decide which class to instantiate. Factory Method defers instantiation of a class to its subclasses.
GoF class diagram:
Code implementation
The first is the product-related interface and implementation class, which is similar to that of a simple factory:// 商品接口 interface Product{ function show() : void; } // 商品实现类A class ConcreteProductA implements Product{ public function show() : void{ echo "I'm A.\n"; } }
// 创建者抽象类 abstract class Creator{ // 抽象工厂方法 abstract protected function FactoryMethod() : Product; // 操作方法 public function AnOperation() : Product{ return $this->FactoryMethod(); } } // 创建者实现类A class ConcreteCreatorA extends Creator{ // 实现操作方法 protected function FactoryMethod() : Product{ return new ConcreteProductA(); } }
Let’s use the analogy of mobile phones again: I am a wholesaler (Client, business side) selling mobile phones. I need a batch of mobile phones (Product A), so I went to get Foxconn ( Factory Creator) to help me produce. I explained my needs to Foxconn, and Foxconn agreed to let my Hengyang factory (ConcreteCreatorA) handle it. There is no need to go to the main factory. Your small order is just a waste. Then after a while, I needed another model of mobile phone (Product B). Foxconn looked at it and asked Zhengzhou Foxconn (Concrete Creator B) to help me produce it. Anyway, no matter what, they always gave me the corresponding mobile phone. Moreover, the Zhengzhou factory does not know what the Hengyang factory has produced or whether it has cooperated with me. Only I and the main factory know all this.
Complete code: Factory method patternhttps://github.com/zhangyue0503/designpatterns-php/blob/master/02.factory/source/factory. php
SMS sending class diagram
Code implementation
<?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; } } abstract class MessageFactory{ abstract protected function factoryMethod(); public function getMessage(){ return $this->factoryMethod(); } } class AliYunFactory extends MessageFactory{ protected function factoryMethod(){ return new AliYunMessage(); } } class BaiduYunFactory extends MessageFactory{ protected function factoryMethod(){ return new BaiduYunMessage(); } } class JiguangFactory extends MessageFactory{ protected function factoryMethod(){ return new JiguangMessage(); } } // 当前业务需要使用百度云 $factory = new BaiduYunFactory(); $message = $factory->getMessage(); echo $message->send('您有新的短消息,请查收');
Complete Source code: SMS sending factory methodhttps://github.com/zhangyue0503/designpatterns-php/blob/master/02.factory/source/factory-message.php
Explanation
PHP Video Tutorial》
The above is the detailed content of A brief discussion on the factory method pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!