In the State Pattern a class will change it's behavior when circumstances change.
In this example, the BookContext class holds an implementation of the BookTitleStateInterface, starting with BookTitleStateStars. BookTitleStateStars and BookTitleStateExclaim will then replace each other in BookContext depending on how many times they are called.
PHP Design Patterns State
About the State
BookContext.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('Book.php');
include_once('BookTitleStateStars.php');
class BookContext {
private $book = NULL;
private $bookTitleState = NULL;
//bookList is not instantiated at construct time
public function __construct($book_in) {
$this->book = $book_in;
$this->setTitleState(new BookTitleStateStars());
}
public function getBookTitle() {
return $this->bookTitleState->showTitle($this);
}
public function getBook() {
return $this->book;
}
public function setTitleState($titleState_in) {
$this->bookTitleState = $titleState_in;
}
}
download source, use right-click and "Save Target As..." to save with a .php extension.
BookTitleStateInterface.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
interface BookTitleStateInterface { public function showTitle($context_in);
}
download source, use right-click and "Save Target As..." to save with a .php extension.
BookTitleStateExclaim.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('Book.php');
include_once('BookTitleStateInterface.php');
include_once('BookTitleStateStars.php');
class BookTitleStateExclaim implements BookTitleStateInterface {
private $titleCount = 0;
public function showTitle($context_in) {
$title = $context_in->getBook()->getTitle();
$this->titleCount++;
$context_in->setTitleState(new BookTitleStateStars());
return Str_replace(' ','!',$title);
}
}
download source, use right-click and "Save Target As..." to save with a .php extension.
BookTitleStateStars.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('Book.php');
include_once('BookTitleStateInterface.php');
include_once('BookTitleStateExclaim.php');
class BookTitleStateStars implements BookTitleStateInterface {
private $titleCount = 0;
public function showTitle($context_in) {
$title = $context_in->getBook()->getTitle();
$this->titleCount++;
if (1 < $this->titleCount) {
$context_in->setTitleState(new BookTitleStateExclaim);
}
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.
testState.php
//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('Book.php'); include_once('bookContext.php');
define('BR', '<'.'BR'.'>');
echo 'BEGIN TESTING STATE PATTERN'.BR; echo BR; $book = new Book('PHP for Cats','Larry Truett');; $context = new bookContext($book); echo 'test 1 - show name'.BR; echo $context->getBookTitle(); echo BR.BR;
echo 'test 2 - show name'.BR; echo $context->getBookTitle(); echo BR.BR; echo 'test 3 - show name'.BR; echo $context->getBookTitle(); echo BR.BR; echo 'test 4 - show name'.BR; echo $context->getBookTitle(); echo BR.BR;
echo 'END TESTING STATE PATTERN'.BR;
download source, use right-click and "Save Target As..." to save with a .php extension.
output of testState.php
BEGIN TESTING STATE PATTERN
test 1 - show name PHP*for*Cats
test 2 - show name PHP*for*Cats
test 3 - show name PHP!for!Cats
test 4 - show name PHP*for*Cats
END TESTING STATE PATTERN
References
| Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate. |
| Comment by Larry on 2012-08-27 Rate this Comment |
This is the first pattern example I've done for PHP which is based on the same pattern example I did for Java. This example and Java Design Patterns State are almost identical. |
| Sign in to comment on PHP Design Patterns State. |