PHP Design Patterns .

PHP Design Patterns Command

About the Command

In the Command Pattern an object encapsulates everything needed to execute a method in another object.

In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object's bookStarsOn() function when it's execute() function is called.

BookCommandee.php

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

class BookCommandee {

private $author; private $title;

function __construct($title_in, $author_in) { $this->setAuthor($author_in); $this->setTitle($title_in); }

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

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

function setStarsOn() { $this->setAuthor(Str_replace(' ','*',$this->getAuthor())); $this->setTitle(Str_replace(' ','*',$this->getTitle())); }

function setStarsOff() { $this->setAuthor(Str_replace('*',' ',$this->getAuthor())); $this->setTitle(Str_replace('*',' ',$this->getTitle())); }

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

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

BookCommand.php

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

include_once('BookCommandee.php');

abstract class BookCommand {

protected $bookCommandee;

function __construct($bookCommandee_in) { $this->bookCommandee = $bookCommandee_in; }

abstract function execute();

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

BookStarsOnCommand.php

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

include_once('BookCommand.php');

class BookStarsOnCommand extends BookCommand {

function execute() {$this->bookCommandee->setStarsOn();}

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

BookStarsOffCommand.php

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

include_once('BookCommand.php');

class BookStarsOffCommand extends BookCommand {

function execute() {$this->bookCommandee->setStarsOff();}

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

testCommand.php

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

include_once('BookCommandee.php'); include_once('BookCommand.php'); include_once('BookStarsOnCommand.php'); include_once('BookStarsOffCommand.php');

echo tagins("html"); echo tagins("head"); echo tagins("/head"); echo tagins("body");

echo "BEGIN TESTING COMMAND PATTERN"; echo tagins("br").tagins("br"); $book = new BookCommandee("Design Patterns", "Gamma, Helm, Johnson, and Vlissides"); echo "book after creation: "; echo tagins("br"); echo $book->getAuthorAndTitle(); echo tagins("br").tagins("br"); $starsOn = new BookStarsOnCommand($book); callCommand($starsOn); echo "book after stars on: "; echo tagins("br"); echo $book->getAuthorAndTitle(); echo tagins("br").tagins("br"); $starsOff = new BookStarsOffCommand($book); callCommand($starsOff); echo "book after stars off: "; echo tagins("br"); echo $book->getAuthorAndTitle(); echo tagins("br");

echo tagins("br"); echo "END TESTING COMMAND PATTERN"; echo tagins("br");

echo tagins("/body"); echo tagins("/html"); //the callCommand function demonstrates that a specified // function in BookCommandee can be executed with only // an instance of BookCommand. function callCommand(BookCommand $bookCommand_in) { $bookCommand_in->execute(); }

//doing this so code can be displayed without breaks function tagins($stuffing) { return "<".$stuffing.">"; }
To download source right-click here and "Save As...".

output of testCommand.php

BEGIN TESTING COMMAND PATTERN

book after creation: Design Patterns by Gamma, Helm, Johnson, and Vlissides

book after stars on: Design*Patterns by Gamma,*Helm,*Johnson,*and*Vlissides

book after stars off: Design Patterns by Gamma, Helm, Johnson, and Vlissides

END TESTING COMMAND 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 bhawin13 on 2013-05-22 Rate this Comment

Hi There,

First comment, good work.

Few points of improvement
1. BookStarsOnCommand , BookStarsOffCommand both command missing constructor which sets BookCommandee object inside it self. If you add it can make clear understanding.
2. callCommand function can be presented like command executer class which is independent of command.

 
Sign in to comment on PHP Design Patterns Command.