PHP Classes

File: src/Strategy/

Recommend this page to a friend!
  Classes of Ogbemudia Osayawe   Head First Design Patterns PHP   src/Strategy/   Download  
File: src/Strategy/
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Head First Design Patterns PHP
PHP version of the code examples of the book
Author: By
Last change:
Date: 3 years ago
Size: 983 bytes


Class file image Download

Strategy pattern

Defines a family of algorithms, encapsulate each one and make them interchangeable.Strategy lets the algorithm vary independently of client that use it.

Design Principle

  • Identify the aspects of your application that vary and separate them from what stay the same
  • Program to an interface, not an implementation
  • Favor composition over inheritance


The example code is for a simple SimUDuck app

    use App\Strategy\Duck\FlyRocketPowered;
    use App\Strategy\Duck\MallardDuck;
    use App\Strategy\Duck\ModelDuck;

    $duck = new MallardDuck();
   // model duck don't know how to fly at creation 
    $model = new ModelDuck();
    // enable model duck to fly using a rocket 
    $model->setFlyBehaviour(new FlyRocketPowered());