Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Strategize

konrad_126
November 22, 2019

 Strategize

konrad_126

November 22, 2019
Tweet

More Decks by konrad_126

Other Decks in Programming

Transcript

  1. TYPES OF PLAYERS <?php class Worker extends Player { }

    class Settler extends Player { } class Warrior extends Player { }
  2. EACH PLAYER CAN <?php class Player { public function greet()

    { // say hello } public function move() { // walk } public function attack() { // punch } }
  3. EVERY PLAYER CAN: SAY HELLO WALK ATTACK TYPES OF PLAYERS:

    WARRIOR WORKER SETTLER <?php class Worker extends Player { } class Settler extends Player { } class Warrior extends Player { public function attack() { // attack with axe } }
  4. DESIGN PRINCIPLE IDENTIFY THE ASPECTS OF YOUR APPLICATION THAT VARY

    AND SEPARATE THEM FROM WHAT STAYS THE SAME
  5. <?php interface MoveBehaviour { public function move(); } class Walk

    implements MoveBehaviour { public function move() { // walk } } class Run implements MoveBehaviour { public function move() { // run } } class RideHorse implements MoveBehaviour { public function move() { // ride a horse } }
  6. <?php interface AttackBehaviour { public function attack(); } class Punch

    implements AttackBehaviour { public function attack() { // punch } } class AxeAttack implements AttackBehaviour { public function attack() { // attack with axe } } class EyePoke implements AttackBehaviour { public function attack() { // poke in eye } }
  7. <?php class Player { private MoveBehaviourInterface $moveBehaviour; private AttackBehaviourInterface $attackBehaviour;

    // ... public function move() { $this->moveBehaviour->move(); } public function attack() { $this->attackBehaviour->attack(); } }
  8. <?php class Player { private MoveBehaviour $moveBehaviour; private AttackBehaviour $attackBehaviour;

    public function __construct(MoveBehaviour $move, AttackBehaviour $attack) { $this->moveBehaviour = $moveBehaviour; $this->attackBehaviour = $attackBehaviour; } // ... }
  9. <?php class Player{ // ... public function setAttackBehaviour(AttackBehaviour $attackBehaviour){ $this->attackBehaviour

    = $attackBehaviour; } // ... } SETTING BEHAVIOUR DYNAMICALLY <?php // ... $explorer = new Explorer(new Run(),new EyePoke()); // explorer found a sword $explorer->setAttackBehaviour(new SwordAttack());
  10. DEFINES A FAMILY OF ALGORITHMS, ENCAPSULATES EACH ONE, AND MAKES

    THEM INTERCHANGABLE THE STRATEGY PATTERN