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); }

}
download source, use right-click and "Save Target As..." to save with a .php extension.

StrategyInterface.php

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

interface StrategyInterface { public function showTitle($book_in);

}
download source, use right-click and "Save Target As..." to save with a .php extension.

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);
    }
  }
download source, use right-click and "Save Target As..." to save with a .php extension.

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); } }
download source, use right-click and "Save Target As..." to save with a .php extension.

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); } }
download source, use right-click and "Save Target As..." to save with a .php extension.

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(); }

}
download source, use right-click and "Save Target As..." to save with a .php extension.


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;
download source, use right-click and "Save Target As..." to save with a .php extension.

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 Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by Larry on 2012-11-05 Rate this Comment

This is the second pattern example I've done for PHP which is based on the same pattern I did for Java.

This example borrows heavily from Java Design Patterns Strategy.

 
Sign in to comment on PHP Design Patterns Strategy.