PHP Design Patterns .

PHP Design Patterns Strategy

About the Strategy

In the Strategy Pattern a context will choose the appropriate concrete extension of a class interface.

In this example, the StrategyContext class will set a strategy of StrategyCaps, StrategyExclaim, or StrategyStars depending on a paramter StrategyContext receives at instantiation. When the showName() method is called in StrategyContext it will call the showName() method in the Strategy that it set.


StrategyContext.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
  
  include_once('StrategyCaps.php');
  include_once('StrategyExclaim.php');
  include_once('StrategyStars.php');  

class StrategyContext { private $strategy = NULL; //bookList is not instantiated at construct time public function __construct($strategy_ind_id) { switch ($strategy_ind_id) { case "C": $this->strategy = new StrategyCaps(); break; case "E": $this->strategy = new StrategyExclaim(); break; case "S": $this->strategy = new StrategyStars(); break; } }

public function showBookTitle($book) { return $this->strategy->showTitle($book); }

}
To download source right-click here and "Save As...".

StrategyInterface.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

interface StrategyInterface { public function showTitle($book_in);

}
To download source right-click here and "Save As...".

StrategyCaps.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
  
  include_once('Book.php');
  include_once('StrategyInterface.php');
  
  class StrategyCaps implements StrategyInterface {
    
    public function showTitle($book_in) {
      $title = $book_in->getTitle();
      $this->titleCount++;
      return strtoupper ($title);
    }
  }
To download source right-click here and "Save As...".

StrategyExclaim.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

include_once('Book.php'); include_once('StrategyInterface.php'); class StrategyExclaim implements StrategyInterface { public function showTitle($book_in) { $title = $book_in->getTitle(); $this->titleCount++; return Str_replace(' ','!',$title); } }
To download source right-click here and "Save As...".

StrategyStars.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

include_once('Book.php'); include_once('StrategyInterface.php'); class StrategyStars implements StrategyInterface { public function showTitle($book_in) { $title = $book_in->getTitle(); $this->titleCount++; return Str_replace(' ','*',$title); } }
To download source right-click here and "Save As...".

Book.php

//copyright Lawrence Truett and FluffyCat.com 2005, all rights reserved
  
  class Book {

private $author; private $title;

function __construct($title_in, $author_in) { $this->author = $author_in; $this->title = $title_in; }

function getAuthor() {return $this->author;}

function getTitle() {return $this->title;}

function getAuthorAndTitle() { return $this->getTitle() . ' by ' . $this->getAuthor(); }

}
To download source right-click here and "Save As...".


testStrategy.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

include_once('Book.php'); include_once('StrategyContext.php');

define('BR', '<'.'BR'.'>');

echo 'BEGIN TESTING STRATEGY PATTERN'.BR; echo BR; $book = new Book('PHP for Cats','Larry Truett'); $strategyContextC = new StrategyContext('C'); $strategyContextE = new StrategyContext('E'); $strategyContextS = new StrategyContext('S'); echo 'test 1 - show name context C'.BR; echo $strategyContextC->showBookTitle($book); echo BR.BR;

echo 'test 2 - show name context E'.BR; echo $strategyContextE->showBookTitle($book); echo BR.BR; echo 'test 3 - show name context S'.BR; echo $strategyContextS->showBookTitle($book); echo BR.BR;

echo 'END TESTING STRATEGY PATTERN'.BR;
To download source right-click here and "Save As...".

output of testStrategy.php

BEGIN TESTING STRATEGY PATTERN

test 1 - show name context C PHP FOR CATS

test 2 - show name context E PHP!for!Cats

test 3 - show name context S PHP*for*Cats

END TESTING STRATEGY PATTERN

References


Design Patterns
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

PHP 5
The Official PHP web site
Core PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
Comments
Sign in to be the first to comment on PHP Design Patterns Strategy.