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;
    }
}
//testFile.php
include_once 'Book.class.php';
include_once 'StrategyContext.class.php';
$book = new Book('This book has a very very long title today.', 'Larry Johnson');
$strategyContextC = new StrategyContext('C');
$strategyContextE = new StrategyContext('E');
$strategyContextS = new StrategyContext('S');
echo $strategyContextC->showTitle($book);
// This book has a very very long title today.
echo $strategyContextE->showTitle($book);
// This!book!!has!!a!!very!!very!!long!!title!!today.
echo $strategyContextS->showTitle($book);
// This book**has**a**very**very**long**title**today.
Exemplo n.º 2
0
        return $this->author;
    }
    function getTitle()
    {
        return $this->title;
    }
    function getAuthorAndTitle()
    {
        return $this->getTitle() . ' by ' . $this->getAuthor();
    }
}
writeln('BEGIN TESTING STRATEGY PATTERN');
writeln('');
$book = new Book('PHP for Cats', 'Larry Truett');
$strategyContextC = new StrategyContext('C');
$strategyContextE = new StrategyContext('E');
$strategyContextS = new StrategyContext('S');
writeln('test 1 - show name context C');
writeln($strategyContextC->showBookTitle($book));
writeln('');
writeln('test 2 - show name context E');
writeln($strategyContextE->showBookTitle($book));
writeln('');
writeln('test 3 - show name context S');
writeln($strategyContextS->showBookTitle($book));
writeln('');
writeln('END TESTING STRATEGY PATTERN');
function writeln($line_in)
{
    echo $line_in . "<br/>";
}