您的当前位置:首页>全部文章>文章详情

php设计模式之策略模式(Strategy)

发表于:2019-08-16 10:32:45浏览:51次TAG:
策略模式主要为了让客户类能够更好地使用某些算法而不需要知道其具体的实现。 ``` _strategy = $strategy; } public function handle_question() { // 根据 传入的 Strategy[具体策略对象] 来执行不同的do_method方法 $this->_strategy->do_method(); } } // client // 使用 策略A $strategyA = new ConcreteStrategyA(); // 实例化 环境角色 $question = new Question($strategyA); // 调用 环境角色 中的 方法,并执行 策略A中的 do_method 方法 $question->handle_question(); // 输出: string(11) "do method A" $strategyB = new ConcreteStrategyB(); $question = new Question($strategyB); $question->handle_question(); // 输出: string(11) "do method B" $strategyC = new ConcreteStrategyC(); $question = new Question($strategyC); $question->handle_question(); // 输出: string(11) "do method C" ``` 输出 ``` vagrant@vagrant-ubuntu:~$ sudo php strategy.php string(11) "do method A" string(11) "do method B" string(11) "do method C" ```
栏目分类全部>
腾讯云采购季云服务器一折促销