LEARNING DESIGN PATTERNS

STRATEGY

ZVONIMIR SPAJIC

BUILD A SIMPLE GAME

3 types of players:

- WARRIOR

- WORKER 

- SETTLER

EVERY PLAYER CAN:

- SAY HELLO

- move 

- ATTACK

3 types of players:

- WARRIOR

- WORKER 

- SETTLER

<?php

class Worker extends Player{
    
}

class Settler extends Player{
   
}

class Warrior extends Player{
    
}

EACH PLAYER CAN:

- SAY HELLO

- move

- ATTACK

<?php

class Player{
    public function move(){
        // move
    }

    public function attack(){
        // punch
    }

    public function sayHello(){
       echo "Hello";
    }
}

EVERY PLAYER CAN:

- SAY HELLO

- WALK

- ATTACK

3 types of players:

- WARRIOR

- WORKER

- SETTLER

<?php

class Worker extends Player{
    // some extra worker methods
}

class Settler extends Player{
    // some extra settler methods
}

class Warrior extends Player{
    // some extra settler methods

    public function attack(){
        // attack with sword
    }
}

The one constant in software development:

 

CHANGE

 

design principle

Identify the aspects of your application that vary and separate them from what stays the same

<?php

interface MoveBehaviour{
    public function move();
}

class Walk implements MoveBehaviour{
    public function move(){
        // move
    }
}

class Run implements MoveBehaviour{
    public function move(){
        // run
    }
}

class RideHorse implements MoveBehaviour{
    public function move(){
        // ride a horse
    }
}
<?php

interface AttackBehaviour{
    public function move();
}

class Punch implements AttackBehaviour{
    public function move(){
        // punch
    }
}

class SwordAttack implements AttackBehaviour{
    public function move(){
        // attack with sword
    }
}

class NoAttack implements AttackBehaviour{
    public function move(){
        // do nothing
    }
}

design principle

 program to an interface, not an implementation

<?php

class Player{

    private $moveBehaviour;
    private $attackBehaviour;

    public function __construct(MoveBehaviour $move, AttackBehaviour $attack){
        $this->moveBehaviour = $moveBehaviour;
        $this->attackBehaviour = $attackBehaviour;
    }

   
    public function performMove(){
        $this->moveBehaviour->move();
    }

    public function performAttack(){
        $this->attackBehaviour->attack();
    }

    //
}

design principle

Favour composition over inheritance

<?php



$player = new Explorer(new Run(),new NoAttack());

$player = new Worker(new Walk(),new Punch());


CREATING PLAYERS

(USE FACTORY PATTERN)

change ?

 

<?php

class Player{

    // ... 

    public function setAttackBehaviour(MoveBehaviour $moveBehaviour){
        $this->moveBehaviour = $moveBehaviour;
    }

    public function setAttackBehaviour(AttackBehaviour $attackBehaviour){
        $this->attackBehaviour = $attackBehaviour;
    }

    // ...
}

SETTING BEHAVIOUR DYNAMICALLY

 

<?php

// ...

$explorer = new Explorer(new Run(),new NoAttack());

// explorer found a sword
$explorer->setBehaviour(new SwordAttackBehaviour());

the strategy pattern

defines a family of algorithms, encapsulates each one, and makes them interchengable

the strategy pattern

 

Thank You

QUESTIONS?

resources

 

design patterns

 

head first design patterns

Made with Slides.com