首页 > Web开发 > 详细

PHP 设计模式之工厂模式

时间:2020-12-28 14:28:11      阅读:23      评论:0      收藏:0      [点我收藏+]


工厂模式

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 设计模式之工厂模式

原文:https://www.cnblogs.com/linsonga/p/14201141.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!