Home  >  Article  >  Backend Development  >  Learn PHP design patterns PHP implementation strategy pattern (strategy)_php skills

Learn PHP design patterns PHP implementation strategy pattern (strategy)_php skills

WBOY
WBOYOriginal
2016-05-16 20:03:39884browse

1. Intention
Define a series of algorithms, encapsulate them one by one, and make them interchangeable. Strategy pattern allows the algorithm to vary independently of the clients using it
What changes in the strategy mode is the algorithm
2. Strategy pattern structure diagram

3. Main characters in strategy mode
Abstract Strategy (Strategy) role: defines the public interface for all supported algorithms. Usually implemented as an interface or abstraction. Context uses this interface to call the algorithm defined by its ConcreteStrategy
ConcreteStrategy role: implement a specific algorithm using the Strategy interface
Environment (Context) role: holds a reference to the Strategy class and uses a ConcreteStrategy object to configure
4. Advantages and disadvantages of strategy mode
Advantages of Strategy Mode:
1. Strategy mode provides a way to manage related algorithm families
2. Strategy pattern provides a way to replace the inheritance relationship by enclosing the calculation in an independent Strategy class so that you can change it independently of its Context
3. Using strategy mode can avoid using multiple conditional transfer statements.
Disadvantages of Strategy Mode:
1. Customers must understand all strategies. This is a potential shortcoming of the strategy model
2. Communication overhead between Strategy and Context
3. Strategy mode will create many strategy categories
5. Applicable Scenarios of Strategy Mode
1. Many related classes simply behave differently. "Strategy" provides a way to configure a class with one of multiple behaviors
2. Different variants of an algorithm need to be used.
3. The algorithm uses data that customers are not supposed to know. Strategy patterns can be used to avoid exposing complex, algorithm-related data structures
4. A class defines multiple behaviors, and these behaviors appear in multiple forms in the operations of this class. Move relevant conditional branches into their respective Strategy classes to replace these conditional statements
6. Strategy mode and other modes
Template pattern: The difference between the template method pattern and the strategy pattern is that the strategy pattern uses delegated methods to provide different algorithmic behaviors, while the template method uses inherited methods to provide different algorithmic behaviors
Flyweight mode (flyweight mode): If multiple client objects need to call the same strategy class, they can implement flyweight mode
7. Strategy Mode PHP Example

<&#63;php
/**
 * 抽象策略角色,以接口实现
 */
interface Strategy {
 
  /**
   * 算法接口
   */
  public function algorithmInterface();
}
 
/**
 * 具体策略角色A
 */
class ConcreteStrategyA implements Strategy {
 
  public function algorithmInterface() {
    echo 'algorithmInterface A<br />';
  }
}
 
/**
 * 具体策略角色B
 */
class ConcreteStrategyB implements Strategy {
 
  public function algorithmInterface() {
    echo 'algorithmInterface B<br />';
  }
}
 
/**
 * 具体策略角色C
 */
class ConcreteStrategyC implements Strategy {
 
  public function algorithmInterface() {
    echo 'algorithmInterface C<br />';
  }
}
 
/**
 * 环境角色
 */
class Context {
  /* 引用的策略 */
  private $_strategy;
 
  public function __construct(Strategy $strategy) {
    $this->_strategy = $strategy;
  }
 
  public function contextInterface() {
    $this->_strategy->algorithmInterface();
  }
 
}
 
/**
 * 客户端
 */
class Client {
 
  /**
   * Main program.
   */
  public static function main() {
    $strategyA = new ConcreteStrategyA();
    $context = new Context($strategyA);
    $context->contextInterface();
 
    $strategyB = new ConcreteStrategyB();
    $context = new Context($strategyB);
    $context->contextInterface();
 
    $strategyC = new ConcreteStrategyC();
    $context = new Context($strategyC);
    $context->contextInterface();
  }
 
}
 
Client::main();
&#63;>

The above is the code for using PHP to implement the strategy pattern. There are also some conceptual distinctions about the strategy pattern. I hope it will be helpful to everyone's learning.

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