Learn PHP Design patterns PHP Implementation strategy mode (strategy) _php tips

Source: Internet
Author: User
Tags learn php

I. Intention
Define a series of algorithms that encapsulate them each, and make them interchangeable. The policy pattern allows the algorithm to vary independently from the customer who uses it
The change of strategy pattern is the algorithm
Second, the strategic pattern structure chart

iii. key roles in the strategy model
Abstract policy (strategy) role: A public interface that defines all supported algorithms. is usually implemented as an interface or an abstraction. The context uses this interface to invoke its concretestrategy-defined algorithm
Specific policy (concretestrategy) Role: Implement a specific algorithm with strategy interface
Environment (context) Role: holds a reference to a strategy class and uses a Concretestrategy object to configure
Iv. Advantages and disadvantages of the strategy model
Advantages of the policy model:
1, the strategy model provides a way to manage the related algorithm family
2. The policy model provides a way to replace an 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 the use of multiple conditional transfer statements.
Disadvantages of the policy model:
1. Customers must understand all the strategies this is a potential drawback of the strategy model
2. Communication overhead between the strategy and the context
3, the policy model will cause a lot of policy classes
v. The application of the policy model scenario
1, many of the related classes are only behavioral differences. "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. You can use policy patterns to avoid exposing complex, algorithmic-related data structures
4, a class defines a variety of behaviors, and these behaviors appear in multiple forms in the operation of this class. Move related conditional branches and their respective strategy classes in place of these conditional statements
VI. strategic models and other models
template mode:The difference between the template method pattern and the policy pattern is that the policy pattern uses the delegated method to provide different algorithm behavior, while the template method uses the inherited method to provide different algorithm behavior
enjoy meta mode (Flyweight mode):If you have multiple client objects that need to invoke the same policy class, you can enable them to implement the meta pattern
Seven, the Policy model PHP sample

<?php/** * Abstract policy role, with interface implementation * * Interface Strategy {/** * algorithm interface/Public function algorithminterface ();} /** * Specific policy role A */class Concretestrategya implements strategy {public Function algorithminterface () {echo ' ALG
  Orithminterface a<br/> ';  }/** * Specific policy role B */class CONCRETESTRATEGYB implements strategy {public Function algorithminterface () {echo
  ' Algorithminterface b<br/> ';  }/** * Specific policy role c */class CONCRETESTRATEGYC implements strategy {public Function algorithminterface () {echo
  ' Algorithminterface c<br/> ';
 
  }/** * Environment role */class Context {*/* referenced policy/private $_strategy;
  Public function __construct (strategy $strategy) {$this->_strategy = $strategy;
  Public Function Contextinterface () {$this->_strategy->algorithminterface ();
   }/** * Client/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 ();?>

This is the code that uses PHP to implement the policy pattern, and there are some conceptual distinctions about the strategy model that I hope will help you learn.

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.