PHP Classes

File: src/Strategy/doc.md

Recommend this page to a friend!
  Classes of Ogbemudia Osayawe  >  Head First Design Patterns PHP  >  src/Strategy/doc.md  >  Download  
File: src/Strategy/doc.md
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: 1 year ago
Size: 983 bytes
 

Contents

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

Usage:

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();
    $duck->fly();
    $duck->quack();
    $duck->display();
    $duck->swim();
    
   // model duck don't know how to fly at creation 
    $model = new ModelDuck();
    $model->fly();
    $model->quack();
    
    // enable model duck to fly using a rocket 
    $model->setFlyBehaviour(new FlyRocketPowered());
    $model->fly();
    $model->display();
    $model->swim();
    
For more information send a message to info at phpclasses dot org.