Home  >  Article  >  Backend Development  >  Head First-Strategy Mode

Head First-Strategy Mode

WBOY
WBOYOriginal
2016-08-08 09:30:501160browse

Strategy pattern, what is strategy pattern, defines a family of algorithms and encapsulates them separately so that they can be replaced with each other. This pattern makes the changes of the algorithm independent of the customers who use the algorithm.

Let’s use ducks to explain the strategy model. Ducks have two behaviors: quacking and flying. However, not all ducks can quack and fly, so we extract these two behaviors that give changes.

<?php
abstract class Duck{
    public $flyBehavior;
    public $quackBehavior;    

    public function __construct(){
    }

    public function performFly(){
        $this->flyBehavior->fly();
    }

    public function performQuack(){
        $this->quackBehavior->quack();
    }

    public function setFlyBehavior(FlyBehavior $fb){
        $this->flyBehavior = $fb;
    }

    public function setQuackBehavior(QuackBehavior $qb){
        $this->quackBehavior = $qb;
    }

    public function swim(){

    }
    
    abstract function display();
}

interface FlyBehavior{
    public function fly();
}
class FlywithWings implements FlyBehavior{
    public function fly(){
        echo "i'm flying!\n";
    }
}
class FlyNoWay implements FlyBehavior{
    public function fly(){
        echo "i can't fly.\n";
    }
}
class FlyRocketPowered implements FlyBehavior{
    public function fly(){
        echo "i'm flying with a rocket!\n";
    }
}

interface QuackBehavior{
    public function quack();
}
class Quack implements QuackBehavior{
    public function quack(){
        echo "quack!\n";
    }
}
class MuteQuack implements QuackBehavior{
    public function quack(){
        echo "silence\n";
    }
}


class MallardDuck extends Duck{
    public function __construct(){
        $this->quackBehavior = new Quack();
        $this->flyBehavior = new FlyNoWay();
    }

    public function display(){
        echo "i'm a real mallar duck\n";
    }    
}

$duck = new MallardDuck;
$duck->performFly();
$duck->setFlyBehavior(new FlyRocketPowered);
$duck->performFly();
?>

From the above code, we can see that we have abstracted the duck, and the flight behavior and quacking driving are in the form of interfaces. The design principle is to use more combinations and less inheritance. Using the above writing method, it is relatively more flexible and not only encapsulates the algorithm Into classes, you can "dynamically change behavior at runtime", as long as the combined behavior object meets the correct interface standard.

The above introduces the Head First-strategy model, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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