php Design Patterns - Strategy Pattern

PHP Strategy Pattern

1. What is the strategy pattern?

Definition: The behavior of a particular set of algorithms and packaged as such, in order to accommodate certain contexts, and so they may be replaced with each other, this model is the strategy pattern.

Simple to understand is that there are n practice for your choice, get the results you need to choose a policy based on

2. Why use the strategy pattern?

After the design pattern used, our code redundancy is low and the degree of coupling, each module performs a function corresponding policy .

Of course, the drawback is a feature we have to make a response strategy class, but we have comprehensive view of MVC architecture which (ThinkPHP, Yii2), a controller corresponds to a view, in fact, reflects the strategy design pattern.

3, the sample code

Business scene

In an electricity supplier site, according to the user's gender to show different commodities. We have to show categories of goods (function showCategory ()) and ad impressions (function showAd ())

Method to realize

First we have to define an interface need to implement the method defined in the interface.

And interface methods are defined to achieve different classes.

Finally, depending on the user, and different methods are new.

/ * * 
 * @Purpose: parent Interface 
 * Interface the UserInterface 
 * / 
interface the UserInterface 
{ 
    / * * 
     * @purpose: Display 
     * Mixed @return 
     * / 
    public  function showAd (); 

    / * * 
     * @purpose: Display Categories 
     * Mixed @return 
     * / 
    public  function showCatetory (); 
} 

/ * * 
 * @purpose: male users implement a user interface 
 * class MaleUser 
 * / 
class MaleUser the implements the UserInterface 
{ 
    / * * 
     * @purpose: display
     Mixed @return * 
     * / 
    public  function showAd () 
    { 
        echo "electronics" ; 
    } 

    / * * 
     * @purpose: Display Categories 
     * Mixed @return 
     * / 
    public  function showCatetory () 
    { 
        echo "digital computer" ; 
    } 
} 

/ * * 
 * @purpose: female users implement user interface 
 * class FemaleUser 
 * / 
class FemaleUser the implements the UserInterface 
{ 
    / * * 
     * @purpose: display 
     * Mixed @return 
     * / 
    public  function showAd () 
    {
         echo "cosmetic" ; 
    } 

    / * * 
     * @purpose: Display Categories 
     * Mixed @return 
     * / 
    public  function showCatetory () 
    { 
        echo "garment categorization" ; 
    } 
} 

IF ( $ _GET [ 'MALE' ] ) {
     $ user = new new MaleUser (); 
} the else {
     $ user = new new FemaleUser (); 
} 
$ user -> showAd ();   // will display their ad content according to the user's gender

4. Summary

Overall, our design principles in development as follows:

  • Find applications may require changes in place, put them independent, and do not need to change the code that mix;
  • An interface for programming, not for the realization of programming;
  • Multi-purpose combination, less inheritance.

This article summarizes the kangaroo learning, if reproduced please indicate the source: https://www.cnblogs.com/chrdai/p/11184082.html

Guess you like

Origin www.cnblogs.com/chrdai/p/11184082.html