search
HomeBackend DevelopmentPHP TutorialPHP设计模式--策略模式

声明:本系列博客参考资料《大话设计模式》,作者程杰。

       策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化,即封装变化的算法。

      

       适用场景:

       1、 多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为。

       2、 需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现。

       3、 对客户隐藏具体策略(算法)的实现细节,彼此完全独立。

       4、客户端必须知道所有的策略类,并自行决定使用哪一个策略类,策略模式只适用于客户端知道所有的算法或行为的情况。

       5、 策略模式造成很多的策略类,每个具体策略类都会产生一个新类。

       有时候可以通过把依赖于环境的状态保存到客户端里面,可以使用享元模式来减少对象的数量。


       UML类图:

      


         角色分析:        

         抽象策略角色(RotateItem):策略类,通常由一个接口或者抽象类实现。

        具体策略角色(ItemX):包装了相关的算法和行为。

        环境角色(ItemContext):持有一个策略类的引用,最终给客户端调用。

 

        具体代码实现:

        

<?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/5/16 * Time: 21:46 *//**抽象策略角色 * Interface RotateItem */interface RotateItem{    function inertiaRotate();    function unInertisRotate();}/**具体策略角色??X产品 * Class XItem */class XItem implements RotateItem{    function inertiaRotate()    {        echo "我是X产品,我惯性旋转了。<br/>";    }    function unInertisRotate()    {        echo "我是X产品,我非惯性旋转了。<br>";    }}/**具体策略角色??Y产品 * Class YItem */class YItem implements RotateItem{    function inertiaRotate()    {        echo "我是Y产品,我<span style="color: #ff0000;">不能</span>惯性旋转。<br>";    }    function unInertisRotate()    {        echo "我是Y产品,我非惯性旋转了。<br>";    }}/**具体策略角色??XY产品 * Class XYItem */class XYItem implements RotateItem{    function inertiaRotate()    {        echo "我是XY产品,我惯性旋转。<br>";    }    function unInertisRotate()    {        echo "我是XY产品,我非惯性旋转了。<br>";    }}class contextStrategy{    private $item;    function getItem($item_name)    {        try        {            $class=new ReflectionClass($item_name);            $this->item=$class->newInstance();        }        catch(ReflectionException $e)        {            $this->item="";        }    }    function inertiaRotate()    {        $this->item->inertiaRotate();    }    function unInertisRotate()    {        $this->item->unInertisRotate();    }}

          客户端调用代码:

          

<?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/5/16 * Time: 21:46 */header("Content-Type:text/html;charset=utf-8");require_once "./Strategy/Strategy.php";$strategy=new contextStrategy();echo "<span style='color: #ff0000;'>X产品<hr>";$strategy->getItem('XItem');$strategy->inertiaRotate();$strategy->unInertisRotate();echo "<span style="color: #ff0000;">Y产品</span><hr>";$strategy->getItem('YItem');$strategy->inertiaRotate();$strategy->unInertisRotate();echo "<span style="color: #ff0000;">XY产品</span><hr>";$strategy->getItem('XYItem');$strategy->inertiaRotate();$strategy->unInertisRotate();

            优点:      

            1、 策略模式提供了管理相关的算法族的办法。

             策略类的等级结构定义了一个算法或行为族。

             恰当使用继承可以把公共的代码转移到父类里面,从而避免重复的代码。

            2、 策略模式提供了可以替换继承关系的办法。

               继承可以处理多种算法或行为。

               如果不是用策略模式,那么使用算法或行为的环境类就可能会有一些子类,每一个子类提供一个不同的算法或行为。但是,这样一来算法或行为的使用者就和算法或行为本身混在一起。决定使用哪一种算法或采取哪一种行为的逻辑就和算法或行为的逻辑混合在一起,从而不可能再独立演化。继承使得动态改变算法或行为变得不可能。

           3、 使用策略模式可以避免使用多重条件转移语句。

             多重转移语句不易维护,它把采取哪一种算法或采取哪一种行为的逻辑与算法或行为的逻辑混合在一起,统统列在一个多重转移语句里面,比使用继承的办法还要原始和落后。


           缺点:

            1、客户端必须知道所有的策略类,并自行决定使用哪一个策略类。

             这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法类。换言之,策略模式只适用于客户端知道所有的算法或行为的情况。

            2、 策略模式造成很多的策略类,每个具体策略类都会产生一个新类。

            有时候可以通过把依赖于环境的状态保存到客户端里面,而将策略类设计成可共享的,这样策略类实例可以被不同客户端使用。换言之,可以使用享元模式来减少对象的数量。

PHP面向对象设计模式
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function