Advertisement
terorama

Patterns / Behaviorial

Dec 9th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.50 KB | None | 0 0
  1. <?php
  2.  
  3. /*----------------------------------------------------------------
  4.                 Chain of responsibility
  5. ----------------------------------------------------------------*/
  6. interface IChain {
  7.  
  8.    function setNextResponsible($el);
  9.    function __call($command, $args);
  10. }
  11. //--------------------------------
  12. abstract class Composite {
  13.  
  14.    private $els;
  15.      
  16.    public function add(IChain $el) {
  17.       $els[] = $el;
  18.       $el->setNextResponsible($this);
  19.    }
  20.    public function get($id) {
  21.       return $els[$id];
  22.    }
  23. }
  24. //--------------------------------
  25. class Blog extends Composite  {
  26.    
  27.     private $blog_info;
  28.    
  29.     public function __construct($in_blog_info) {
  30.    
  31.        $this->blog_info = $in_blog_info;
  32.     }
  33.     public function the_blog_info() {
  34.        echo $this->blog_info.'<br/>';
  35.     }
  36.    
  37. }
  38. //--------------------------------
  39. class Post extends Composite implements IChain {
  40.  
  41.    private $next_responsible;
  42.    private $post_id;
  43.  
  44.    public function __construct($id) {
  45.       $this->post_id = $id;
  46.    }  
  47.    public function the_post_info() {
  48.       echo 'post: '.$this->post_id.'<br/>';
  49.    }
  50.    
  51.    public function setNextResponsible($el) {
  52.       $this->next_responsible = $el;
  53.    }
  54.    
  55.     public function __call($command, $args) {
  56.       if (!method_exists($this, $command)) {
  57.          $this->next_responsible->$command();
  58.       }
  59.    }
  60.    
  61. }
  62. //--------------------------------
  63. class Comment implements IChain {
  64.    
  65.    private $next_responsible;
  66.    private $comment_id;
  67.    
  68.    public function __construct($id) {
  69.       $this->comment_id = $id;
  70.    }
  71.    
  72.    public function the_comment_info() {
  73.       echo 'comment: '.$this->comment_id.'<br/>';
  74.    }
  75.    
  76.    public function setNextResponsible($el) {
  77.       $this->next_responsible = $el;
  78.    }
  79.    
  80.    public function __call($command, $args) {
  81.       if (!method_exists($this, $command)) {
  82.          $this->next_responsible->$command();
  83.       }
  84.    }
  85. }
  86.  
  87. //--------------------------------
  88. echo 'Begin testing CHAIN OF RESPONSIBILITY pattern<br/>';
  89.  
  90. $blog = new Blog('test blog');
  91.  
  92. $post = new Post(4);
  93. $blog->add($post);
  94.  
  95. $comment = new Comment(5);
  96. $post->add($comment);
  97.  
  98. echo 'comment requests<br/>';
  99. $comment->the_comment_info();
  100. $comment->the_post_info();
  101. $comment->the_blog_info();
  102.  
  103. echo 'post requests<br/>';
  104. $post->the_post_info();
  105. $post->the_blog_info();
  106.  
  107. /*----------------------------------------------------------------
  108.                         Command
  109. ----------------------------------------------------------------*/
  110. abstract class Command {
  111.    
  112.    protected $commandee;
  113.    
  114.    public function __construct($commandee_in) {
  115.       $this->commandee = $commandee_in;
  116.    }
  117.    
  118.    abstract function execute();
  119. }
  120.  
  121. //--------------------------------
  122. class Post {
  123.  
  124.    private $post_id;
  125.    private $fancy = '%s';
  126.    
  127.    public function __construct($id) {
  128.       $this->post_id = $id;
  129.    }
  130.    
  131.    public function fancyOn() {
  132.       $this->fancy = '<b>%s</b>';
  133.    }
  134.    public function fancyOff() {
  135.       $this->fancy = '%s';
  136.    }
  137.    public function draw() {
  138.       echo sprintf($this->fancy, 'post: '.$this->post_id).'<br/>';
  139.    }
  140. }
  141. //--------------------------------
  142. class FancyOnCommand extends Command {
  143.  
  144.    public function execute() {
  145.       $this->commandee->fancyOn();
  146.    }
  147. }
  148. //--------------------------------
  149. class FancyOffCommand extends Command {
  150.    
  151.    public function execute() {
  152.       $this->commandee->fancyOff();
  153.    }
  154. }
  155.  
  156. //--------------------------------
  157. echo 'Begin testing COMMAND pattern<br/>';
  158.  
  159. $post = new Post(4);
  160. $post->draw();
  161.  
  162. $fancy_on_command = new FancyOnCommand($post);
  163. $fancy_off_command = new FancyOffCommand($post);
  164.  
  165. $fancy_on_command->execute();
  166. $post->draw();
  167. $fancy_off_command->execute();
  168. $post->draw();
  169.  
  170. /*----------------------------------------------------------------
  171.                         Iterator
  172. ----------------------------------------------------------------*/
  173. class Post {
  174.    private $post_id;
  175.    
  176.    public function __construct($id) {
  177.       $this->post_id = $id;
  178.    }
  179.    public function draw() {
  180.       echo 'post: '.$this->post_id.'<br/>';
  181.    }
  182. }
  183.  
  184. //--------------------------------
  185. class Blog {
  186.    private $posts = array();
  187.    
  188.    public function getCount() {
  189.       return count($this->posts);
  190.    }
  191.    
  192.    public function getPost($index) {
  193.    
  194.       return $this->posts[$index];
  195.    }
  196.    
  197.    public function addPost($post) {
  198.       $this->posts[] = $post;
  199.    }
  200.  
  201. }
  202. //--------------------------------
  203. class BlogIterator {
  204.    protected $blog;
  205.    protected $current = -1;
  206.    
  207.    public function __construct(Blog $in_blog) {
  208.       $this->blog = $in_blog;
  209.    }  
  210.    public function get_current() {    
  211.       return ($this->current<$this->blog->getCount()) ? $this->blog->getPost($this->current) : new Post(null);
  212.    }
  213.    public function has_next() {
  214.       return $this->current+1<$this->blog->getCount();
  215.    }
  216.    public function has_previous() {
  217.       return $this->current-1>=0;
  218.    }
  219.    public function get_next() {    
  220.       return $this->has_next() ? $this->get_current(++$this->current) :  new Post(null);;
  221.    }  
  222.    public function get_previous() {
  223.       return $this->has_previous() ? $this->get_current(--$this->current) :  new Post(null);;
  224.    }
  225.  
  226. }
  227. //--------------------------------
  228. echo 'Begin testing ITERATOR pattern<br/>';
  229.  
  230. $blog = new Blog();
  231. $blog->addPost( new Post(4));
  232. $blog->addPost( new Post(5));
  233. $blog->addPost( new Post(6));
  234. $blog->addPost( new Post(7));
  235.  
  236. $iterator = new BlogIterator($blog);
  237.  
  238. while ($iterator->has_next()) {
  239.     $iterator->get_next()->draw();
  240. }
  241.  
  242. $iterator->get_next()->draw();
  243. $iterator->get_next()->draw();
  244.  
  245. $iterator->get_current()->draw();
  246.  
  247. while ($iterator->has_previous()) {
  248.    $iterator->get_previous()->draw();
  249. }
  250.  
  251. /*----------------------------------------------------------------
  252.                         Mediator
  253. ----------------------------------------------------------------*/
  254. abstract class CentralizedComposite {
  255.    
  256.    protected $mediator;
  257.    protected $element_id;
  258.    
  259.    private $els = array();
  260.  
  261.    public function __construct($id, $in_mediator) {
  262.    
  263.       $this->mediator = $in_mediator;
  264.      
  265.       $this->element_id = $id;
  266.    }
  267.    public function add($el) {
  268.       $this->els[] = $el;
  269.      
  270.       return $this;
  271.    }
  272.    public function getAll() {
  273.       return $this->els;
  274.    }
  275.    
  276. }
  277. //--------------------------------
  278. class BlogMediator {
  279.  
  280.    public function opened($el) {
  281.    
  282.       if ($el instanceof Blog) {
  283.          foreach ($el->getAll() as $subel) {
  284.             if ($subel instanceof Post) {
  285.                $subel->open_post();
  286.             }
  287.          }
  288.       }
  289.       if ($el instanceof Post) {
  290.          foreach ($el->getAll() as $subel) {
  291.             if ($subel instanceof Comment) {
  292.                $subel->open_comment();
  293.             }
  294.          }
  295.       }
  296.    }
  297.    //------------------
  298.    public function closed($el) {
  299.      
  300.       if ($el instanceof Blog) {
  301.          foreach ($el->getAll() as $subel) {         
  302.             if ($subel instanceof Post) {
  303.                $subel->close_post();
  304.             }
  305.          }
  306.       }
  307.       if ($el instanceof Post) {
  308.          foreach ($el->getAll() as $subel) {
  309.             if ($subel instanceof Comment) {
  310.                $subel->close_comment();
  311.             }
  312.          }
  313.       }
  314.    }
  315. }
  316. //--------------------------------
  317. class Blog extends CentralizedComposite {
  318.    
  319.    private $closed = false;
  320.    
  321.    public function open_blog() {
  322.       $this->closed = false;
  323.       $this->mediator->opened($this);
  324.    }
  325.    public function close_blog() {
  326.       $this->closed = true;
  327.       $this->mediator->closed($this);
  328.    }
  329.    
  330.    public function draw() {
  331.       echo 'blog '.$this->element_id.' '.($this->closed ? 'closed' : 'open').'<br/>';
  332.      
  333.       foreach ($this->getAll() as $post) {    
  334.          $post->draw();
  335.       }
  336.    }
  337. }
  338.  
  339. //--------------------------------
  340. class Post extends CentralizedComposite {
  341.  
  342.    private $closed = false;
  343.    
  344.    public function open_post() {
  345.       $this->closed = false;
  346.       $this->mediator->opened($this);
  347.    }
  348.    public function close_post() {
  349.       $this->closed = true;
  350.       $this->mediator->closed($this);
  351.    }
  352.    
  353.    public function draw() {
  354.       echo str_repeat('&nbsp;',3).'post '.$this->element_id.' '.($this->closed ? 'closed' : 'open').'<br/>';
  355.      
  356.       foreach ($this->getAll() as $comment) {
  357.          $comment->draw();
  358.       }
  359.    }
  360. }
  361.  
  362. //--------------------------------
  363. class Comment {
  364.  
  365.    private $closed = false;
  366.    
  367.    public function open_comment() {
  368.       $this->closed = false;
  369.      
  370.    }
  371.    public function close_comment() {
  372.       $this->closed = true;
  373.      
  374.    }
  375.    
  376.    public function draw() {
  377.       echo str_repeat('&nbsp;',6).'comment  '.($this->closed ? 'closed' : 'open').'<br/>';
  378.    }
  379. }
  380. //--------------------------------
  381. echo 'Begin testing MEDIATOR pattern<br/>';
  382.  
  383. $mediator = new BlogMediator();
  384.  
  385. $blog = new Blog(8, $mediator);
  386. $post4 = new Post(4, $mediator);
  387. $post5 = new Post(5, $mediator);
  388.  
  389. $comment45 = new Comment(45, $mediator);
  390. $comment46 = new Comment(46, $mediator);
  391. $comment55 = new Comment(55, $mediator);
  392. $comment56 = new Comment(56, $mediator);
  393.  
  394. $post4->add($comment45)->add($comment46);
  395. $post5->add($comment55)->add($comment56);
  396.  
  397. $blog->add($post4)->add($post5);
  398.  
  399. $blog->draw();
  400. echo '<br/>';
  401.  
  402. $post4->close_post();
  403. $comment55->close_comment();
  404. $blog->draw();
  405. echo '<br/>';
  406.  
  407. $blog->close_blog();
  408. $blog->draw();
  409.  
  410. /*----------------------------------------------------------------
  411.                         Memento
  412. ----------------------------------------------------------------*/
  413.  
  414.  
  415. /*----------------------------------------------------------------
  416.                         Visitor
  417. ----------------------------------------------------------------*/
  418. interface IVisitee {
  419.    function accept(IVisitor $visitor_in);
  420. }
  421.  
  422. interface IVisitor {
  423.  
  424.    function visit_post (Post $visitee);
  425.    function visit_comment (Comment $visitee);
  426. }
  427.  
  428. //--------------------------------
  429. class Post implements IVisitee {
  430.    
  431.    private $post_id;
  432.    
  433.    public function __construct($id) {
  434.       $this->post_id = $id;
  435.    }
  436.    public function get_postid() {
  437.       return $this->post_id;
  438.    }
  439.    
  440.    public function accept(IVisitor $visitor_in) {
  441.       $visitor_in->visit_post($this);
  442.    }
  443. }
  444. //--------------------------------
  445. class Comment implements IVisitee {
  446.  
  447.    private $comment_id;
  448.    
  449.    public function __construct($id) {    
  450.       $this->comment_id = $id;
  451.    }
  452.    public function get_commentid() {
  453.       return $this->comment_id;
  454.    }
  455.    
  456.    public function accept(IVisitor $visitor_in) {
  457.       $visitor_in->visit_comment($this);
  458.    }
  459. }
  460.  
  461. //--------------------------------
  462. class plainVisitor implements IVisitor {
  463.    
  464.    public function visit_post(Post $visitee) {
  465.       echo 'post: '.$visitee->get_postid().'<br/>';
  466.    }
  467.    
  468.    public function visit_comment(Comment $visitee) {
  469.       echo 'comment: '.$visitee->get_commentid().'<br/>';
  470.    }
  471. }
  472. //--------------------------------
  473. class fancyVisitor implements IVisitor {
  474.    
  475.    public function visit_post(Post $visitee) {
  476.       echo '<b>post: '.$visitee->get_postid().'</b><br/>';
  477.    }
  478.    
  479.    public function visit_comment(Comment $visitee) {
  480.       echo '<b>comment: '.$visitee->get_commentid().'</b><br/>';
  481.    }
  482. }
  483.  
  484. //--------------------------------
  485. echo 'Begin testing VISITOR pattern<br/>';
  486.  
  487. $post = new Post(4);
  488. $comment = new Comment(5);
  489.  
  490. $plain_visitor = new plainVisitor();
  491. $fancy_visitor = new fancyVisitor();
  492.  
  493. echo 'test plain visitor<br/>';
  494.  
  495. $post->accept($plain_visitor);
  496. $comment->accept($plain_visitor);
  497.  
  498. echo 'test fancy visitor<br/>';
  499.  
  500. $post->accept($fancy_visitor);
  501. $comment->accept($fancy_visitor);
  502.  
  503.  
  504.  
  505.  
  506. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement