terorama

Patterns / Structural

Jun 9th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.60 KB | None | 0 0
  1. <?php
  2.  
  3. /*----------------------------------------------------------------
  4.                        Adapter
  5. ----------------------------------------------------------------*/
  6. interface outputObject {
  7.    function output();
  8. }
  9.  
  10. //-----------------------------------------------
  11. class SimplePost {
  12.    
  13.    private $post_id;
  14.    
  15.    public function __construct($id) {
  16.       $this->post_id = $id;
  17.    }
  18.    public function drawPost() {
  19.       echo "post {$this->post_id} <br/>\n";
  20.    }
  21. }
  22. //-----------------------------------------------
  23. class PostAdapter implements outputObject {
  24.    
  25.    private $zpost;
  26.    
  27.    public function __construct(SimplePost $inpost) {
  28.       $this->zpost = $inpost;
  29.    }
  30.    
  31.    public function output() {
  32.       $this->zpost->drawPost();
  33.    }
  34. }
  35. //-----------------------------------------------
  36.  
  37. echo "Begin testing ADAPTER pattern<br/>\n";
  38.  
  39. $post_4 = new SimplePost(4);
  40. $post_adapter = new PostAdapter($post_4);
  41. $post_adapter->output();
  42.  
  43. /*----------------------------------------------------------------
  44.                        Bridge
  45. ----------------------------------------------------------------*/
  46.  
  47. abstract class AbstractPost {
  48.    
  49.    protected $post_id;
  50.    protected $postImplement;
  51.    
  52.    public function __construct($id ) {
  53.    
  54.       $this->post_id = $id;  
  55.    }
  56.    
  57.    public function setImplementation ( AbstractPostImplementation $in_implement) {
  58.      
  59.        $this->postImplement = $in_implement;
  60.    }  
  61.    
  62.    abstract function show();
  63. }
  64.  
  65. //-----------------------------------------------
  66. abstract class AbstractPostImplementation {
  67.  
  68.    abstract function getTitle($id);
  69.    abstract function getExcerpt($id);
  70. }
  71.  
  72. //-----------------------------------------------
  73. class PlainPost extends AbstractPost {
  74.  
  75.    public function show() {
  76.    
  77.       echo $this->postImplement->getTitle($this->post_id)."<br/>\n";
  78.       echo $this->postImplement->getExcerpt($this->post_id)."<br/>\n";
  79.    }
  80. }
  81. //-----------------------------------------------
  82. class TabularPost extends AbstractPost {
  83.  
  84.    public function show() {
  85.    
  86.       echo '<table><tr><td>'.$this->postImplement->getTitle($this->post_id).
  87.            '</td></tr><tr><td>'.$this->postImplement->getExcerpt($this->post_id).
  88.            '</td></tr></table>';
  89.    }
  90. }
  91. //-----------------------------------------------
  92. class SimplePostImplementation extends AbstractPostImplementation {
  93.  
  94.    public function getTitle($id) {
  95.       return 'post '.$id;
  96.    }
  97.    public function getExcerpt($id) {
  98.       return 'Lorem Ipsum';
  99.    }
  100. }
  101.  
  102. //-----------------------------------------------
  103. class FancyPostImplementation extends AbstractPostImplementation {
  104.  
  105.    public function getTitle($id) {
  106.       return '<b>post '.$id.'</b>';
  107.    }
  108.    public function getExcerpt($id) {
  109.       return '<b>Lorem Ipsum</b>';
  110.    }
  111. }
  112.  
  113. //-----------------------------------------------
  114. echo "Begin testing BRIDGE pattern<br/>\n";
  115.  
  116. $post_4 = new PlainPost(4);
  117. $post_5 = new TabularPost(5);
  118.  
  119. $simple_implement = new SimplePostImplementation();
  120. $fancy_implement = new FancyPostImplementation();
  121.  
  122.  
  123. echo "testing plain / simple<br/>\n";
  124.  
  125. $post_4->setImplementation($simple_implement);
  126. $post_4->show();
  127.  
  128. echo "testing plain / fancy<br/>\n";
  129.  
  130. $post_4->setImplementation($fancy_implement);
  131. $post_4->show();
  132.  
  133. echo "testing tabular / simple<br/>\n";
  134.  
  135. $post_5->setImplementation($simple_implement);
  136. $post_5->show();
  137.  
  138. echo "testing tabular / fancy<br/>\n";
  139.  
  140. $post_5->setImplementation($fancy_implement);
  141. $post_5->show();
  142.  
  143. /*----------------------------------------------------------------
  144.                        Composite
  145. ----------------------------------------------------------------*/
  146. interface IComments {
  147.    
  148.    function getComment($id);
  149.    
  150.    function addComment($id, $isgroup=false);
  151.    function deleteComment($id);
  152.    
  153.    function draw();
  154.    
  155. }
  156.  
  157. //-----------------------------------------------
  158. class oneComment implements IComments {
  159.  
  160.    private $comment_id;
  161.  
  162.    public function __construct($id) {
  163.       $this->comment_id = $id;
  164.    }
  165.    
  166.    public function addComment($id, $isgroup=false) {
  167.       return false;
  168.    }
  169.    public function deleteComment($id) {
  170.       return false;
  171.    }
  172.    public function getComment($id) {  
  173.       return false;
  174.    }
  175.    
  176.    public function draw() {
  177.       echo 'comment '.$this->comment_id."<br/>\n";
  178.    }
  179. }
  180. //-----------------------------------------------
  181. class groupComments implements IComments {
  182.  
  183.    private $group_id;
  184.    private $zcomments = array();
  185.    
  186.    public function __construct($id) {
  187.       $this->group_id = $id;
  188.    }
  189.    
  190.    public function addComment($id, $isgroup=false) {
  191.    
  192.       if ($isgroup)
  193.          $el = new groupComments($id);
  194.       else
  195.          $el = new oneComment($id);
  196.          
  197.       $this->zcomments[$id] = $el;
  198.    }
  199.    
  200.    public function deleteComment($id) {
  201.    
  202.       unset($this->zcomments[$id]);
  203.    }
  204.    
  205.    public function getComment($id) {
  206.    
  207.       if (isset($this->zcomments[$id]))
  208.           return $this->zcomments[$id];
  209.          
  210.       foreach ($this->zcomments as $key=>$comm) {
  211.          $inf = $comm->getComment($id);
  212.          if ($inf!==false)
  213.             return $inf;
  214.       }
  215.       return false;
  216.      
  217.    }
  218.    
  219.    public function draw() {
  220.       echo 'group '.$this->group_id."<br/>\n";
  221.    }
  222. }
  223.  
  224. echo "Begin testing COMPOSITE pattern<br/>\n";
  225.  
  226. $gr_4 = new groupComments(4);
  227. $gr_4->addComment(5,true);
  228. $gr_4->getComment(5)->addComment(6);
  229. $gr_4->addComment(7);
  230.  
  231. $gr_4->draw();
  232. $gr_4->getComment(5)->draw();
  233. $gr_4->getComment(6)->draw();
  234. $gr_4->getComment(7)->draw();
  235.  
  236.  
  237.  
  238. /*----------------------------------------------------------------
  239.                        Decorator
  240. ----------------------------------------------------------------*/
  241.  
  242. interface IDraw {
  243.    function draw();
  244. }
  245.  
  246. //-----------------------------------------------
  247. class Post implements IDraw {
  248.  
  249.    private $post_id;
  250.      
  251.    public function __construct($id) {
  252.    
  253.       $this->post_id = $id;
  254.    }
  255.    
  256.    public function _gettext() {
  257.       return "post {$this->post_id}";
  258.    }
  259.    
  260.    public function _draw($intext) {
  261.       echo $intext;
  262.    }
  263.  
  264.    public function draw() {
  265.       $this->_draw($this->_gettext()."<br/>\n");
  266.    }
  267. }
  268.  
  269. //-----------------------------------------------
  270. abstract class Decorator implements IDraw {
  271.    
  272.    private $el;
  273.    
  274.  
  275.    public function __construct(IDraw $inel) {
  276.       $this->el = $inel;
  277.    }
  278.    
  279.    public function __call($name, $arguments) {
  280.       if (!method_exists($this, $name)) {
  281.          return call_user_func_array(array($this->el,$name),$arguments);
  282.       }
  283.    }
  284.    
  285.    public function draw() {
  286.    }
  287. }
  288. //-----------------------------------------------
  289. class FancyDecorator extends Decorator {
  290.    
  291.    private $fancyText='%s';
  292.    
  293.    public function setFancyText($deco) {
  294.       $this->fancyText = $deco;
  295.    }
  296.    public function draw() {
  297.       $this->_draw(sprintf($this->fancyText, $this->_gettext())."<br/>\n");
  298.    }
  299. }
  300.  
  301. //-----------------------------------------------
  302. echo "Begin Testing DECORATOR pattern<br/>\n";
  303.  
  304. $post_4 = new Post(4);
  305. $post_4->draw();
  306.  
  307.  
  308. $fancy_decorator = new FancyDecorator($post_4);
  309. $fancy_decorator->setFancyText('<b>%s</b>');
  310. $fancy_decorator->draw();
  311.  
  312.  
  313. /*----------------------------------------------------------------
  314.                          FlyWeight
  315. ----------------------------------------------------------------*/
  316.  
  317. class Post {
  318.  
  319.    private $post_id;
  320.    
  321.    public function __construct($id) {
  322.       $this->post_id = $id;
  323.    }
  324.    
  325.    public function draw() {
  326.       static $n;
  327.       $n++;
  328.       echo "post {$this->post_id} $n<br/>\n";
  329.    }
  330. }
  331. //-----------------------------------------------
  332.  
  333. class FlyweightFactory {
  334.  
  335.    private $post_array = array();
  336.    
  337.    function getPost($id) {
  338.    
  339.       if (!array_key_exists($id, $this->post_array))
  340.          $this->post_array[$id] = new Post($id);
  341.          
  342.       return $this->post_array[$id];
  343.    }
  344.  
  345. }
  346.  
  347. //-----------------------------------------------
  348.  
  349. echo "Begin testing FLYWEIGHT pattern<br/>\n";
  350.  
  351. $flyweight_factory = new FlyweightFactory();
  352.  
  353. $flyweight_factory->getPost(4)->draw();
  354. $flyweight_factory->getPost(4)->draw();
  355.  
  356. $flyweight_factory->getPost(5)->draw();
  357.  
  358. /*----------------------------------------------------------------
  359.                          Facade
  360. ----------------------------------------------------------------*/
  361.  
  362. class Post {
  363.  
  364.    private $post_id;
  365.    
  366.    public function __construct($id) {
  367.    
  368.       $this->post_id = $id;
  369.    }
  370.    
  371.    public function draw() {
  372.       echo FancyFacade::fancy("post {$this->post_id}<br/>\n");
  373.    }
  374. }
  375. //--------------------------------
  376. class FancyFacade {
  377.    
  378.    public static function fancy($s) {
  379.    
  380.       $s = Texturizer::texturize($s);
  381.       $s = Colorizer::colorize($s);
  382.       $s = Fontizer::fontize($s);
  383.      
  384.      
  385.       return $s;
  386.    }
  387. }
  388. //--------------------------------
  389. class Texturizer {
  390.    
  391.    public static function texturize($s) {
  392.       return ucwords(strtolower(str_replace('  ',' ',$s)));
  393.    }
  394. }
  395. class Colorizer {
  396.    public static function colorize($s) {
  397.      
  398.       return preg_replace ('/(\s|^)(\w)/','$1<font color="red">$2</font>',$s);
  399.    }
  400. }
  401. class Fontizer {
  402.  
  403.    public static function fontize($s) {
  404.    
  405.       preg_match_all('/<.*?>/',$s, $matches);
  406.       $s = preg_replace('/<.*?>/','ZUUUZ', $s);
  407.  
  408.       $arr = explode(' ',$s);
  409.      
  410.        foreach ($arr as $key=>$val) {
  411.        
  412.           if (!trim($val))
  413.              continue;
  414.              
  415.           if (substr($val,-5,5)=='ZUUUZ')
  416.              $n = -6;
  417.           else
  418.              $n = -1;
  419.  
  420.           $arr[$key] = substr_replace($val, '<font color="green">'.substr($val,$n,1).'</font>',$n,1);
  421.       }
  422.       $s = implode(' ',$arr);
  423.  
  424.       for ($i=0; $i<count($matches[0]);$i++) {
  425.          $s = substr_replace($s,  $matches[0][$i], strpos($s,'ZUUUZ'),5);
  426.       }
  427.       echo $s;
  428.    }
  429. }
  430.  
  431. //--------------------------------
  432. echo "Begin testing FACADE pattern<br/>\n";
  433. $post = new Post(4);
  434. $post->draw();
  435.  
  436. /*----------------------------------------------------------------
  437.                    Private Class Data
  438. ----------------------------------------------------------------*/
  439.  
  440. class PostData {
  441.  
  442.    private $title;
  443.    private $excerpt;
  444.    private $author;
  445.    
  446.    public function __construct ($title, $excerpt, $author) {
  447.      
  448.       $this->title = $title;
  449.       $this->excerpt = $excerpt;
  450.       $this->author = $author;
  451.    }
  452.    
  453.    public function __get($a) {
  454.       return $this->$a;
  455.    }
  456. }
  457.  
  458. class Post {
  459.  
  460.    private $postData;
  461.    
  462.    public function __construct ($title, $excerpt, $author) {
  463.       $this->postData = new PostData($title, $excerpt, $author);
  464.    }
  465.    
  466.    public function draw() {
  467.       echo $this->postData->title.'<br/>';
  468.       echo $this->postData->excerpt.'<br/>';
  469.       echo $this->postData->author.'<br/>';
  470.    }
  471. }
  472.  
  473. //--------------------------------
  474. echo "Begin testing PRIVATE CLASS DATA pattern<br>\n";
  475.  
  476. $post = new Post('test_title','test_excerpt','test_author');
  477. $post->draw();
  478.  
  479.  
  480. /*----------------------------------------------------------------
  481.                             Proxy
  482. ----------------------------------------------------------------*/
  483.  
  484. class Post {
  485.  
  486.    private $pic_data;
  487.  
  488.    public function __construct($id) {
  489.    
  490.       $db = new mysqli ('','','','');
  491.       if (mysqli_connect_errno()) {
  492.          throw new Exception ('connect error: '.mysqli_connect_error());
  493.       }
  494.      
  495.       $stmt = $db->prepare('select link from bookm_tmp where id=?');
  496.       if ($db->errno!=0) {
  497.          throw new Exception ('db error: '.$db->error);
  498.       }
  499.       $stmt->bind_param('d',$id);
  500.       $stmt->execute();
  501.       $stmt->bind_result($link);
  502.       while ($stmt->fetch()) {
  503.      
  504.          $ch = curl_init();
  505.          curl_setopt($ch,CURLOPT_URL, $link);
  506.          curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  507.          curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
  508.          curl_setopt($ch,CURLOPT_USERAGENT,  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko)'.
  509.                                   ' Chrome/20.0.1132.57 Safari/536.11');
  510.        
  511.          curl_setopt($ch, CURLOPT_REFERER,'http://www.google.com');
  512.          curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
  513.          curl_setopt($ch, CURLOPT_ENCODING, 1);
  514.          
  515.          curl_setopt($ch, CURLOPT_HEADER,0);
  516.          curl_setopt($ch, CURLOPT_NOBODY,0);
  517.          
  518.          curl_setopt($ch, CURLOPT_POST,0);
  519.          //curl_setopt($ch, CURL_POSTFIELDS, $postdata);
  520.          
  521.          //curl_setopt($ch, CURLOPT_COOKIEFILE,'ztest.txt');
  522.          //curl_setopt($ch, CURLOPT_COOKIEJAR,'ztest.txt');  
  523.          //curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
  524.          
  525.          $data = curl_exec($ch);
  526.          curl_close($ch);
  527.          
  528.          $this->pic_data = 'data:image/jpeg;base64,'.base64_encode($data);
  529.          
  530.       }
  531.       $stmt->close();
  532.       $db->close();
  533.    }
  534.    
  535.    public function draw() {
  536.       echo '<br/><img src="'.$this->pic_data.'" /><br/>';
  537.    }
  538. }
  539.  
  540. //------------------------------
  541. class PostProxy extends Post {
  542.    
  543.    private $post_id;
  544.    private $post=null;
  545.    
  546.    
  547.    public function __construct($id) {
  548.    
  549.       $this->post_id = $id;
  550.    }
  551.    
  552.    public function draw() {
  553.    
  554.       if ($this->post==null)
  555.          $this->post = parent::__construct($this->post_id);
  556.          
  557.       parent::draw();
  558.    }
  559. }
  560.  
  561. //------------------------------
  562. echo "Begin testing PROXY pattern<br/>\n";
  563.  
  564. $posts = get_posts();
  565. $posts[4]->draw();
  566. $posts[5]->draw();
  567. $posts[7]->draw();
  568.  
  569. function get_posts() {
  570.  
  571.    $posts = array();
  572.    
  573.    $db = new mysqli ('','','','');
  574.    $rez = $db->query('select id from bookm_tmp order by id limit 1, 20');
  575.    
  576.    while ($row = $rez->fetch_assoc()) {
  577.       $posts[] = new PostProxy($row['id']);
  578.    }
  579.    $rez->free();
  580.    $db->close();
  581.    return $posts;
  582. }
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590. ?>
Add Comment
Please, Sign In to add comment