Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*----------------------------------------------------------------
- Adapter
- ----------------------------------------------------------------*/
- interface outputObject {
- function output();
- }
- //-----------------------------------------------
- class SimplePost {
- private $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function drawPost() {
- echo "post {$this->post_id} <br/>\n";
- }
- }
- //-----------------------------------------------
- class PostAdapter implements outputObject {
- private $zpost;
- public function __construct(SimplePost $inpost) {
- $this->zpost = $inpost;
- }
- public function output() {
- $this->zpost->drawPost();
- }
- }
- //-----------------------------------------------
- echo "Begin testing ADAPTER pattern<br/>\n";
- $post_4 = new SimplePost(4);
- $post_adapter = new PostAdapter($post_4);
- $post_adapter->output();
- /*----------------------------------------------------------------
- Bridge
- ----------------------------------------------------------------*/
- abstract class AbstractPost {
- protected $post_id;
- protected $postImplement;
- public function __construct($id ) {
- $this->post_id = $id;
- }
- public function setImplementation ( AbstractPostImplementation $in_implement) {
- $this->postImplement = $in_implement;
- }
- abstract function show();
- }
- //-----------------------------------------------
- abstract class AbstractPostImplementation {
- abstract function getTitle($id);
- abstract function getExcerpt($id);
- }
- //-----------------------------------------------
- class PlainPost extends AbstractPost {
- public function show() {
- echo $this->postImplement->getTitle($this->post_id)."<br/>\n";
- echo $this->postImplement->getExcerpt($this->post_id)."<br/>\n";
- }
- }
- //-----------------------------------------------
- class TabularPost extends AbstractPost {
- public function show() {
- echo '<table><tr><td>'.$this->postImplement->getTitle($this->post_id).
- '</td></tr><tr><td>'.$this->postImplement->getExcerpt($this->post_id).
- '</td></tr></table>';
- }
- }
- //-----------------------------------------------
- class SimplePostImplementation extends AbstractPostImplementation {
- public function getTitle($id) {
- return 'post '.$id;
- }
- public function getExcerpt($id) {
- return 'Lorem Ipsum';
- }
- }
- //-----------------------------------------------
- class FancyPostImplementation extends AbstractPostImplementation {
- public function getTitle($id) {
- return '<b>post '.$id.'</b>';
- }
- public function getExcerpt($id) {
- return '<b>Lorem Ipsum</b>';
- }
- }
- //-----------------------------------------------
- echo "Begin testing BRIDGE pattern<br/>\n";
- $post_4 = new PlainPost(4);
- $post_5 = new TabularPost(5);
- $simple_implement = new SimplePostImplementation();
- $fancy_implement = new FancyPostImplementation();
- echo "testing plain / simple<br/>\n";
- $post_4->setImplementation($simple_implement);
- $post_4->show();
- echo "testing plain / fancy<br/>\n";
- $post_4->setImplementation($fancy_implement);
- $post_4->show();
- echo "testing tabular / simple<br/>\n";
- $post_5->setImplementation($simple_implement);
- $post_5->show();
- echo "testing tabular / fancy<br/>\n";
- $post_5->setImplementation($fancy_implement);
- $post_5->show();
- /*----------------------------------------------------------------
- Composite
- ----------------------------------------------------------------*/
- interface IComments {
- function getComment($id);
- function addComment($id, $isgroup=false);
- function deleteComment($id);
- function draw();
- }
- //-----------------------------------------------
- class oneComment implements IComments {
- private $comment_id;
- public function __construct($id) {
- $this->comment_id = $id;
- }
- public function addComment($id, $isgroup=false) {
- return false;
- }
- public function deleteComment($id) {
- return false;
- }
- public function getComment($id) {
- return false;
- }
- public function draw() {
- echo 'comment '.$this->comment_id."<br/>\n";
- }
- }
- //-----------------------------------------------
- class groupComments implements IComments {
- private $group_id;
- private $zcomments = array();
- public function __construct($id) {
- $this->group_id = $id;
- }
- public function addComment($id, $isgroup=false) {
- if ($isgroup)
- $el = new groupComments($id);
- else
- $el = new oneComment($id);
- $this->zcomments[$id] = $el;
- }
- public function deleteComment($id) {
- unset($this->zcomments[$id]);
- }
- public function getComment($id) {
- if (isset($this->zcomments[$id]))
- return $this->zcomments[$id];
- foreach ($this->zcomments as $key=>$comm) {
- $inf = $comm->getComment($id);
- if ($inf!==false)
- return $inf;
- }
- return false;
- }
- public function draw() {
- echo 'group '.$this->group_id."<br/>\n";
- }
- }
- echo "Begin testing COMPOSITE pattern<br/>\n";
- $gr_4 = new groupComments(4);
- $gr_4->addComment(5,true);
- $gr_4->getComment(5)->addComment(6);
- $gr_4->addComment(7);
- $gr_4->draw();
- $gr_4->getComment(5)->draw();
- $gr_4->getComment(6)->draw();
- $gr_4->getComment(7)->draw();
- /*----------------------------------------------------------------
- Decorator
- ----------------------------------------------------------------*/
- interface IDraw {
- function draw();
- }
- //-----------------------------------------------
- class Post implements IDraw {
- private $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function _gettext() {
- return "post {$this->post_id}";
- }
- public function _draw($intext) {
- echo $intext;
- }
- public function draw() {
- $this->_draw($this->_gettext()."<br/>\n");
- }
- }
- //-----------------------------------------------
- abstract class Decorator implements IDraw {
- private $el;
- public function __construct(IDraw $inel) {
- $this->el = $inel;
- }
- public function __call($name, $arguments) {
- if (!method_exists($this, $name)) {
- return call_user_func_array(array($this->el,$name),$arguments);
- }
- }
- public function draw() {
- }
- }
- //-----------------------------------------------
- class FancyDecorator extends Decorator {
- private $fancyText='%s';
- public function setFancyText($deco) {
- $this->fancyText = $deco;
- }
- public function draw() {
- $this->_draw(sprintf($this->fancyText, $this->_gettext())."<br/>\n");
- }
- }
- //-----------------------------------------------
- echo "Begin Testing DECORATOR pattern<br/>\n";
- $post_4 = new Post(4);
- $post_4->draw();
- $fancy_decorator = new FancyDecorator($post_4);
- $fancy_decorator->setFancyText('<b>%s</b>');
- $fancy_decorator->draw();
- /*----------------------------------------------------------------
- FlyWeight
- ----------------------------------------------------------------*/
- class Post {
- private $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function draw() {
- static $n;
- $n++;
- echo "post {$this->post_id} $n<br/>\n";
- }
- }
- //-----------------------------------------------
- class FlyweightFactory {
- private $post_array = array();
- function getPost($id) {
- if (!array_key_exists($id, $this->post_array))
- $this->post_array[$id] = new Post($id);
- return $this->post_array[$id];
- }
- }
- //-----------------------------------------------
- echo "Begin testing FLYWEIGHT pattern<br/>\n";
- $flyweight_factory = new FlyweightFactory();
- $flyweight_factory->getPost(4)->draw();
- $flyweight_factory->getPost(4)->draw();
- $flyweight_factory->getPost(5)->draw();
- /*----------------------------------------------------------------
- Facade
- ----------------------------------------------------------------*/
- class Post {
- private $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function draw() {
- echo FancyFacade::fancy("post {$this->post_id}<br/>\n");
- }
- }
- //--------------------------------
- class FancyFacade {
- public static function fancy($s) {
- $s = Texturizer::texturize($s);
- $s = Colorizer::colorize($s);
- $s = Fontizer::fontize($s);
- return $s;
- }
- }
- //--------------------------------
- class Texturizer {
- public static function texturize($s) {
- return ucwords(strtolower(str_replace(' ',' ',$s)));
- }
- }
- class Colorizer {
- public static function colorize($s) {
- return preg_replace ('/(\s|^)(\w)/','$1<font color="red">$2</font>',$s);
- }
- }
- class Fontizer {
- public static function fontize($s) {
- preg_match_all('/<.*?>/',$s, $matches);
- $s = preg_replace('/<.*?>/','ZUUUZ', $s);
- $arr = explode(' ',$s);
- foreach ($arr as $key=>$val) {
- if (!trim($val))
- continue;
- if (substr($val,-5,5)=='ZUUUZ')
- $n = -6;
- else
- $n = -1;
- $arr[$key] = substr_replace($val, '<font color="green">'.substr($val,$n,1).'</font>',$n,1);
- }
- $s = implode(' ',$arr);
- for ($i=0; $i<count($matches[0]);$i++) {
- $s = substr_replace($s, $matches[0][$i], strpos($s,'ZUUUZ'),5);
- }
- echo $s;
- }
- }
- //--------------------------------
- echo "Begin testing FACADE pattern<br/>\n";
- $post = new Post(4);
- $post->draw();
- /*----------------------------------------------------------------
- Private Class Data
- ----------------------------------------------------------------*/
- class PostData {
- private $title;
- private $excerpt;
- private $author;
- public function __construct ($title, $excerpt, $author) {
- $this->title = $title;
- $this->excerpt = $excerpt;
- $this->author = $author;
- }
- public function __get($a) {
- return $this->$a;
- }
- }
- class Post {
- private $postData;
- public function __construct ($title, $excerpt, $author) {
- $this->postData = new PostData($title, $excerpt, $author);
- }
- public function draw() {
- echo $this->postData->title.'<br/>';
- echo $this->postData->excerpt.'<br/>';
- echo $this->postData->author.'<br/>';
- }
- }
- //--------------------------------
- echo "Begin testing PRIVATE CLASS DATA pattern<br>\n";
- $post = new Post('test_title','test_excerpt','test_author');
- $post->draw();
- /*----------------------------------------------------------------
- Proxy
- ----------------------------------------------------------------*/
- class Post {
- private $pic_data;
- public function __construct($id) {
- $db = new mysqli ('','','','');
- if (mysqli_connect_errno()) {
- throw new Exception ('connect error: '.mysqli_connect_error());
- }
- $stmt = $db->prepare('select link from bookm_tmp where id=?');
- if ($db->errno!=0) {
- throw new Exception ('db error: '.$db->error);
- }
- $stmt->bind_param('d',$id);
- $stmt->execute();
- $stmt->bind_result($link);
- while ($stmt->fetch()) {
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL, $link);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
- curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko)'.
- ' Chrome/20.0.1132.57 Safari/536.11');
- curl_setopt($ch, CURLOPT_REFERER,'http://www.google.com');
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
- curl_setopt($ch, CURLOPT_ENCODING, 1);
- curl_setopt($ch, CURLOPT_HEADER,0);
- curl_setopt($ch, CURLOPT_NOBODY,0);
- curl_setopt($ch, CURLOPT_POST,0);
- //curl_setopt($ch, CURL_POSTFIELDS, $postdata);
- //curl_setopt($ch, CURLOPT_COOKIEFILE,'ztest.txt');
- //curl_setopt($ch, CURLOPT_COOKIEJAR,'ztest.txt');
- //curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
- $data = curl_exec($ch);
- curl_close($ch);
- $this->pic_data = 'data:image/jpeg;base64,'.base64_encode($data);
- }
- $stmt->close();
- $db->close();
- }
- public function draw() {
- echo '<br/><img src="'.$this->pic_data.'" /><br/>';
- }
- }
- //------------------------------
- class PostProxy extends Post {
- private $post_id;
- private $post=null;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function draw() {
- if ($this->post==null)
- $this->post = parent::__construct($this->post_id);
- parent::draw();
- }
- }
- //------------------------------
- echo "Begin testing PROXY pattern<br/>\n";
- $posts = get_posts();
- $posts[4]->draw();
- $posts[5]->draw();
- $posts[7]->draw();
- function get_posts() {
- $posts = array();
- $db = new mysqli ('','','','');
- $rez = $db->query('select id from bookm_tmp order by id limit 1, 20');
- while ($row = $rez->fetch_assoc()) {
- $posts[] = new PostProxy($row['id']);
- }
- $rez->free();
- $db->close();
- return $posts;
- }
- ?>
Add Comment
Please, Sign In to add comment