Pages

Search This Blog

Friday 12 June 2015

Implementation Of Strategy Design Pattern in PHP (Practical Example)

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Strategy Design Pattern in PHP (Practical Example)
 * @Date : 12-June-2015
 * @Defination  :  In computer programming, the strategy pattern (also known as the policy pattern) is a
 * software design pattern that enables an algorithm's behavior to be selected at runtime. The strategy pattern
 * defines a family of algorithms,
 * encapsulates each algorithm, and
 * makes the algorithms interchangeable within that family.
 *
 * 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.
 *
 */

class StrategyContext {
   
    private $strategy = NULL;
    // list 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);
    }
}

interface StrategyInterface {
    public function showTitle($book_in);
}

class StrategyCaps implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return strtoupper ($title);
    }
}

class StrategyExclaim implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return Str_replace(' ','!',$title);
    }
}

class StrategyStars implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return Str_replace(' ','*',$title);
    }
}

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

  writeln('BEGIN TESTING STRATEGY PATTERN');
  writeln('');

  $book = new Book('Strategy Design Patern for PHP','Zuhair Mirza');

  $strategyContextC = new StrategyContext('C');
  $strategyContextE = new StrategyContext('E');
  $strategyContextS = new StrategyContext('S');

  writeln('Sample 1 ---> show name context C');
  writeln($strategyContextC->showBookTitle($book));
  writeln('');

  writeln('Sample 2 ---> show name context E');
  writeln($strategyContextE->showBookTitle($book));
  writeln('');

  writeln('Sample 3 ---> show name context S');
  writeln($strategyContextS->showBookTitle($book));
  writeln('');

  writeln('END TESTING STRATEGY PATTERN');

  function writeln($line_in) {
    echo $line_in."<br/>";
  }

No comments:

Post a Comment