. PHP Design Patterns . PHP Design Patterns Command

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


  }


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

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

  }


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

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

  }


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

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

  }


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

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.">";
  }


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

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
Sign In
to add the first comment for PHP Design Patterns Command.