PHP设计模式 - 策略模式

策略模式主要为了让客户类能够更好地使用某些算法而不需要知道其具体的实现。

<?php
interface Strategy { // 抽象策略角色,以接口实现
    public function do_method(); // 算法接口
}

class ConcreteStrategyA implements Strategy { // 具体策略角色A 
    public function do_method() {
        echo 'do method A';
    }
}

class ConcreteStrategyB implements Strategy { // 具体策略角色B 
    public function do_method() {
        echo 'do method B';
    }
}

class ConcreteStrategyC implements Strategy { // 具体策略角色C
    public function do_method() {
        echo 'do method C';
    }
}


class Question{ // 环境角色
    private $_strategy;

    public function __construct(Strategy $strategy) {
        $this->_strategy = $strategy;
    } 
    public function handle_question() {
        $this->_strategy->do_method();
    }
}

// client
$strategyA = new ConcreteStrategyA();
$question = new Question($strategyA);
$question->handle_question();//输出do method A

$strategyB = new ConcreteStrategyB();
$question = new Question($strategyB);
$question->handle_question();//输出do method B

$strategyC = new ConcreteStrategyC();
$question = new Question($strategyC);
$question->handle_question();//输出do method C

 

posted @ 2018-06-25 22:55  share112  阅读(88)  评论(0编辑  收藏  举报