工厂模式
interface Message {
public function send(string $msg);
}
class AliYunMessage implements Message {
public function send(string $msg) {
return ‘阿里云短信(原阿里大鱼)发送成功!短信内容:‘ . $msg;
}
}
class BaiduYunMessage implements Message {
public function send(string $msg) {
return ‘百度SMS短信发送成功!短信内容:‘ . $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();
}
}
// 使用百度
$factory = new BaiduYunFactory();
$message = $factory->getMessage();
echo $message->send(‘您有新的短消息,请查收‘);
PHP 设计模式之工厂模式