PHP Policy Mode implementation

Source: Internet
Author: User
Tags php example

First, the intention
Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. The policy pattern allows the algorithm to change independently of the customers who use it
The strategy pattern changes is the algorithm
Second, the Strategy model structure chart

Third, the main role in the strategy model
Abstract policy (Strategy) role: Defines the public interface for all supported algorithms. is usually implemented as an interface or an abstraction. The context uses this interface to invoke its concretestrategy-defined algorithm
Specific strategy (CONCRETESTRATEGY) Role: Implementing a specific algorithm with strategy interface
Environment (context) Role: holds a reference to a strategy class, configured with a Concretestrategy object
Iv. advantages and disadvantages of the strategy model
Advantages of the Strategy mode:
1, the Strategy mode provides the method to manage the related algorithm family
2. The strategy model provides a way to replace the inheritance relationship by enclosing it in a separate strategy class so that you can change it independently of its context.
3. Use the policy mode to avoid using multiple conditional transfer statements.
Disadvantages of the policy model:
1, the customer must understand all the policies this is a potential disadvantage of the strategy model
2. Communication overhead between strategy and context
3, Policy mode will cause a lot of policy classes
Five, the Strategy mode application scenario
1. A lot of related classes are just behavior-specific. Policy provides a way to configure a class with one behavior in multiple behaviors
2, need to use a different variant of the algorithm.
3, the algorithm uses the data that the customer should not know. Policy patterns can be used to avoid exposing complex, algorithmic-related data structures
4. A class defines a variety of behaviors, and these behaviors occur in multiple forms in the operations of this class. Move related conditional branches and their respective strategy classes in place of these conditional statements
VI. strategy models and other models
Template mode: Unlike policy patterns, the policy pattern uses delegated methods to provide different algorithmic behavior, whereas template methods provide different algorithmic behavior using inherited methods
enjoy meta mode (Flyweight mode): If you have multiple client objects that need to invoke the same class of policy classes, you can make them implement the enjoy meta mode
VII. Policy Mode PHP example

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485 <?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();?>

PHP Policy Mode implementation

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.