Advertisement
krot

[tpl] Composite

Apr 10th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. interface Element{
  7. function prints();
  8.  
  9. }
  10.  
  11. class A implements Element{
  12.     function prints(){
  13.     echo 'A element';
  14.     }
  15. }
  16. class B implements Element{
  17.     function prints(){
  18.     echo 'B element';
  19.     }
  20. }
  21. class C implements Element{
  22.     function prints(){
  23.     echo 'C element';
  24.     }
  25. }
  26. class Composite implements Element{
  27. private $list=array();
  28.    
  29.     function prints(){
  30.         foreach($this->list as $obj){
  31.             if($obj instanceOf Element)$obj->prints();
  32.             else echo $obj;
  33.         }
  34.     }
  35.     function add($e){
  36.         $this->list[]=$e;
  37.     }
  38.  
  39. }
  40.  
  41. $c= new Composite();
  42. $c2= new Composite();
  43. $c2->add(1);
  44. $c2->add(new C());
  45.  
  46. $c->add(new A());
  47. $c->add(new B());
  48.  
  49. $c2->add($c);
  50.  
  51.  
  52. $c2->prints();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement