PHP design mode--strategy mode

Source: Internet
Author: User

Statement: This series of blog reference "Big Talk design mode", author Geoscience.

Policy Mode defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other. The policy pattern makes the algorithm independent of the client using it , i.e. the algorithm that encapsulates the change.


Applicable scenarios:

1 , multiple classes differ only in Performance Behavior different, you can use Strategy mode, in run-time dynamic selection of specific behavior to be performed .

2 , Need use different strategies in different situations ( algorithm ) , or the strategy may be implemented in other ways in the future. .

3 , to customers Hide Specific Policies ( algorithm ) implementation details that are completely independent of each other .

4 , the client must know all the policy classes and decide for itself which policy class to use, and the policy mode only applies to The client knows all the algorithms or the behavior of the case .

5 , policy mode causes a lot of policy classes, each specific policy class will produce a new class.

Sometimes you can use the enjoy meta mode to reduce the number of objects by saving the environment-dependent state to the client.


UML Class Diagrams:


Role Analysis:

Abstract Policy roles ( Rotateitem ): a policy class, usually implemented by an interface or abstract class.

Specific policy roles ( ItemX ): wrapped up related algorithms and behaviors.

Environment Role ( ItemContext ): holds a reference to a policy class that ultimately gives Client called.


Specific code implementation:

<?php/** * Created by Phpstorm. * User:jiang * DATE:2015/5/16 * time:21:46 *//** abstract Policy role * Interface Rotateitem */interface rotateitem{function inert    Iarotate (); function uninertisrotate ();} /** specific policy role--x product * Class Xitem */class Xitem implements rotateitem{function Inertiarotate () {echo "I am an x product, I am the inertia spin Turn it.    <br/> "; } function Uninertisrotate () {echo "I am the x product, I am not inertia rotation."    <br/> "; }}/** specific policy role--y product * Class Yitem */class Yitem implements rotateitem{function Inertiarotate () {echo "I am the y product, I & Lt;span style= ' color: #ff0000; ' > Cannot </span> inertia rotation.    <br/> "; } function Uninertisrotate () {echo "I am the y product, I am not inertia rotation."    <br/> "; }}/** specific policy role--xy product * Class Xyitem */class Xyitem implements rotateitem{function Inertiarotate () {echo ' I'm XY Product, I rotate in inertia.    <br/> "; } function Uninertisrotate () {echo "I am xy product, I am not inertia rotation."    <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 (); }}

Client calling Code:

<?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 products </span>

Advantages:

1 , the policy model provides a way to manage the associated algorithm families.

The hierarchy structure of a policy class defines an algorithm or a family of behaviors.

Proper use of inheritance can transfer common code to the parent class, thus avoiding duplicate code.

2 , the policy model provides a way to replace the inheritance relationship.

Inheritance can handle multiple algorithms or behaviors.

If the policy mode is not used, then the environment class using the algorithm or behavior may have some subclasses, each of which provides a different algorithm or behavior. However, the user of the algorithm or behavior is mixed with the algorithm or the behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change an algorithm or behavior.

3 , use policy mode to avoid using multiple conditional transfer statements.

Multiple transfer statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or the action, all in a multiple transfer statement, more primitive and backward than the method of using inheritance.


Disadvantages:

1 , the client must know all the policy classes and decide for itself which policy class to use.

This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows all the algorithms or behaviors.

2 , the policy pattern causes a lot of policy classes, and each specific policy class produces a new class.

It is sometimes possible to design a policy class to be shareable by saving the environment-dependent state to the client, so that the policy class instance can be used by different clients. In other words, you can use the enjoy meta mode to reduce the number of objects.

PHP Object-oriented design pattern

PHP design mode-policy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.