策略模式,又称为政策模式,属于行为型的设计模式。php
GoF定义:定义一系列的算法,把它们一个个封装起来,而且使它们能够相互替换。本模式使得算法可独立于使用它的客户而变化 。git
GoF类图github
代码实现算法
interface Strategy{
function AlgorithmInterface();
}
class ConcreteStrategyA implements Strategy{
function AlgorithmInterface(){
echo "算法A";
}
}
class ConcreteStrategyB implements Strategy{
function AlgorithmInterface(){
echo "算法B";
}
}
class ConcreteStrategyC implements Strategy{
function AlgorithmInterface(){
echo "算法C";
}
}
复制代码
定义算法抽象及实现。设计模式
class Context{
private $strategy;
function __construct(Strategy $s){
$this->strategy = $s;
}
function ContextInterface(){
$this->strategy->AlgorithmInterface();
}
}
复制代码
定义执行环境上下文。函数
$strategyA = new ConcreteStrategyA();
$context = new Context($strategyA);
$context->ContextInterface();
$strategyB = new ConcreteStrategyB();
$context = new Context($strategyB);
$context->ContextInterface();
$strategyC = new ConcreteStrategyC();
$context = new Context($strategyC);
$context->ContextInterface();
复制代码
最后,在客户端按需调用合适的算法。单元测试
既然和简单工厂如此的相像,那么咱们也按照简单工厂的方式来讲:咱们是一个手机厂商(Client),想找某工厂(ConcreteStrategy)来作一批手机,经过渠道商(Context)向这个工厂下单制造手机,渠道商直接去联系代工厂(Strategy),而且直接将生产完成的手机发货给我(ContextInterface())。一样的,我不用关心他们的具体实现,我只要监督那个和咱们联系的渠道商就能够啦,是否是很省心!测试
完整代码:github.com/zhangyue050…优化
依然仍是短信功能,具体的需求能够参看简单工厂模式中的讲解,可是这回咱们使用策略模式来实现!this
短信发送类图
<?php
interface Message {
public function send();
}
class BaiduYunMessage implements Message {
function send() {
echo '百度云发送信息!';
}
}
class AliYunMessage implements Message {
public function send() {
echo '阿里云发送信息!';
}
}
class JiguangMessage implements Message {
public function send() {
echo '极光发送信息!';
}
}
class MessageContext {
private $message;
public function __construct(Message $msg) {
$this->message = $msg;
}
public function SendMessage() {
$this->message->send();
}
}
$bdMsg = new BaiduYunMessage();
$msgCtx = new MessageContext($bdMsg);
$msgCtx->SendMessage();
$alMsg = new AliYunMessage();
$msgCtx = new MessageContext($alMsg);
$msgCtx->SendMessage();
$jgMsg = new JiguangMessage();
$msgCtx = new MessageContext($jgMsg);
$msgCtx->SendMessage();
复制代码
说明
策略模式算是一个中场休息,后面还有一大半的模式尚未讲呢,接下来登场的这位但是近几年的网红选手:责任链模式。不要告诉我你没听过这位的大名,Laravel的中间件就是这货的典型的实现哦!!