PHP design Pattern--Strategy mode ____php

Source: Internet
Author: User
Tags inheritance mixed

Statement: This series of blog reference "Dahua design Model", the author Geoscience.

The policy pattern defines a series of algorithms and encapsulates each one, and allows them to be replaced with each other. The policy pattern allows the algorithm to be independent of its client, that is, the algorithm that encapsulates the change.

Applicable scenario:

1, multiple classes only differ in the performance behavior, you can use the strategy mode, the runtime dynamically select the behavior to perform.

2. Different policies (algorithms) need to be used in different situations, or strategies may be implemented in other ways in the future.

3, the customer hides the specific strategy (algorithm) The realization details, each other completely independent.

4. The client must know all the policy classes and decide which policy class to use, and the policy pattern applies only to situations where the client knows all the algorithms or behaviors.

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

Sometimes you can reduce the number of objects by saving the state of your environment to the client by using the pattern.


UML Class Diagram:


Role Analysis:

Abstract policy Role (Rotateitem): A policy class, usually implemented by an interface or abstract class.

Specific policy role (ITEMX): Wraps related algorithms and behaviors.

Environment Role (ITEMCONTEXT): holds a reference to a policy class that is eventually invoked to the client.


Specific code implementation:

<?php/** * Created by Phpstorm. * User:jiang * DATE:2015/5/16 * time:21:46 */** abstract policy role * Interface Rotateitem/Interface Rotateitem {func
    tion Inertiarotate ();
function Uninertisrotate (); /** specific policy Role--x product * Class Xitem/class Xitem implements Rotateitem {function inertiarotate () {echo " I am the x product, I am inertia rotation.
    <br/> "; function Uninertisrotate () {echo "I am the x product, I am not inertia rotating."
    <br/> ";  }/** specific policy role--y product * Class Yitem/class Yitem implements Rotateitem {function inertiarotate () {echo "I am the y product, I <span style= ' color: #ff0000; ' > Cannot </span> inertia rotation.
    <br/> "; function Uninertisrotate () {echo "I am a y product, I am not inertia rotation."
    <br/> "; }/** specific policy role--xy product * Class Xyitem/class Xyitem implements {function Rotateitem () {E Cho "I'm xy product, I'm inertia spinning."
    <br/> "; function Uninertisrotate () {echo "I'm an xy product, I'm spinning out of inertia."
    <br/> "; }} Classcontextstrategy {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 Invoke 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 strategy model provides a way to manage the related algorithm family.

The hierarchical structure of a policy class defines an algorithm or a behavior family.

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

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

Inheritance can handle multiple algorithms or behaviors.

If it is not a policy mode, then the environment class that uses the algorithm or behavior may have 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 of deciding which algorithm to use or which behavior to take is mixed with the logic of algorithm or behavior, thus it is impossible to evolve independently. Inheritance makes it impossible to dynamically alter an algorithm or behavior.

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

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


Disadvantages:

1. The client must know all the policy classes and decide which policy class to use.

This means that the client must understand the difference between these algorithms in order to select the appropriate algorithm class 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 model causes a lot of policy classes, each specific policy class will produce a new class.

Sometimes a policy class instance can be used by different clients by saving the state of the environment to the client and by designing it to be sharable. In other words, you can use the element mode to reduce the number of objects.


Please pay attention to my video course, the address is as follows, thank you.


PHP Object-oriented design pattern

Related Article

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.