Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*----------------------------------------------------------------
- Chain of responsibility
- ----------------------------------------------------------------*/
- interface IChain {
- function setNextResponsible($el);
- function __call($command, $args);
- }
- //--------------------------------
- abstract class Composite {
- private $els;
- public function add(IChain $el) {
- $els[] = $el;
- $el->setNextResponsible($this);
- }
- public function get($id) {
- return $els[$id];
- }
- }
- //--------------------------------
- class Blog extends Composite {
- private $blog_info;
- public function __construct($in_blog_info) {
- $this->blog_info = $in_blog_info;
- }
- public function the_blog_info() {
- echo $this->blog_info.'<br/>';
- }
- }
- //--------------------------------
- class Post extends Composite implements IChain {
- private $next_responsible;
- private $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function the_post_info() {
- echo 'post: '.$this->post_id.'<br/>';
- }
- public function setNextResponsible($el) {
- $this->next_responsible = $el;
- }
- public function __call($command, $args) {
- if (!method_exists($this, $command)) {
- $this->next_responsible->$command();
- }
- }
- }
- //--------------------------------
- class Comment implements IChain {
- private $next_responsible;
- private $comment_id;
- public function __construct($id) {
- $this->comment_id = $id;
- }
- public function the_comment_info() {
- echo 'comment: '.$this->comment_id.'<br/>';
- }
- public function setNextResponsible($el) {
- $this->next_responsible = $el;
- }
- public function __call($command, $args) {
- if (!method_exists($this, $command)) {
- $this->next_responsible->$command();
- }
- }
- }
- //--------------------------------
- echo 'Begin testing CHAIN OF RESPONSIBILITY pattern<br/>';
- $blog = new Blog('test blog');
- $post = new Post(4);
- $blog->add($post);
- $comment = new Comment(5);
- $post->add($comment);
- echo 'comment requests<br/>';
- $comment->the_comment_info();
- $comment->the_post_info();
- $comment->the_blog_info();
- echo 'post requests<br/>';
- $post->the_post_info();
- $post->the_blog_info();
- /*----------------------------------------------------------------
- Command
- ----------------------------------------------------------------*/
- abstract class Command {
- protected $commandee;
- public function __construct($commandee_in) {
- $this->commandee = $commandee_in;
- }
- abstract function execute();
- }
- //--------------------------------
- class Post {
- private $post_id;
- private $fancy = '%s';
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function fancyOn() {
- $this->fancy = '<b>%s</b>';
- }
- public function fancyOff() {
- $this->fancy = '%s';
- }
- public function draw() {
- echo sprintf($this->fancy, 'post: '.$this->post_id).'<br/>';
- }
- }
- //--------------------------------
- class FancyOnCommand extends Command {
- public function execute() {
- $this->commandee->fancyOn();
- }
- }
- //--------------------------------
- class FancyOffCommand extends Command {
- public function execute() {
- $this->commandee->fancyOff();
- }
- }
- //--------------------------------
- echo 'Begin testing COMMAND pattern<br/>';
- $post = new Post(4);
- $post->draw();
- $fancy_on_command = new FancyOnCommand($post);
- $fancy_off_command = new FancyOffCommand($post);
- $fancy_on_command->execute();
- $post->draw();
- $fancy_off_command->execute();
- $post->draw();
- /*----------------------------------------------------------------
- Iterator
- ----------------------------------------------------------------*/
- class Post {
- private $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function draw() {
- echo 'post: '.$this->post_id.'<br/>';
- }
- }
- //--------------------------------
- class Blog {
- private $posts = array();
- public function getCount() {
- return count($this->posts);
- }
- public function getPost($index) {
- return $this->posts[$index];
- }
- public function addPost($post) {
- $this->posts[] = $post;
- }
- }
- //--------------------------------
- class BlogIterator {
- protected $blog;
- protected $current = -1;
- public function __construct(Blog $in_blog) {
- $this->blog = $in_blog;
- }
- public function get_current() {
- return ($this->current<$this->blog->getCount()) ? $this->blog->getPost($this->current) : new Post(null);
- }
- public function has_next() {
- return $this->current+1<$this->blog->getCount();
- }
- public function has_previous() {
- return $this->current-1>=0;
- }
- public function get_next() {
- return $this->has_next() ? $this->get_current(++$this->current) : new Post(null);;
- }
- public function get_previous() {
- return $this->has_previous() ? $this->get_current(--$this->current) : new Post(null);;
- }
- }
- //--------------------------------
- echo 'Begin testing ITERATOR pattern<br/>';
- $blog = new Blog();
- $blog->addPost( new Post(4));
- $blog->addPost( new Post(5));
- $blog->addPost( new Post(6));
- $blog->addPost( new Post(7));
- $iterator = new BlogIterator($blog);
- while ($iterator->has_next()) {
- $iterator->get_next()->draw();
- }
- $iterator->get_next()->draw();
- $iterator->get_next()->draw();
- $iterator->get_current()->draw();
- while ($iterator->has_previous()) {
- $iterator->get_previous()->draw();
- }
- /*----------------------------------------------------------------
- Mediator
- ----------------------------------------------------------------*/
- abstract class CentralizedComposite {
- protected $mediator;
- protected $element_id;
- private $els = array();
- public function __construct($id, $in_mediator) {
- $this->mediator = $in_mediator;
- $this->element_id = $id;
- }
- public function add($el) {
- $this->els[] = $el;
- return $this;
- }
- public function getAll() {
- return $this->els;
- }
- }
- //--------------------------------
- class BlogMediator {
- public function opened($el) {
- if ($el instanceof Blog) {
- foreach ($el->getAll() as $subel) {
- if ($subel instanceof Post) {
- $subel->open_post();
- }
- }
- }
- if ($el instanceof Post) {
- foreach ($el->getAll() as $subel) {
- if ($subel instanceof Comment) {
- $subel->open_comment();
- }
- }
- }
- }
- //------------------
- public function closed($el) {
- if ($el instanceof Blog) {
- foreach ($el->getAll() as $subel) {
- if ($subel instanceof Post) {
- $subel->close_post();
- }
- }
- }
- if ($el instanceof Post) {
- foreach ($el->getAll() as $subel) {
- if ($subel instanceof Comment) {
- $subel->close_comment();
- }
- }
- }
- }
- }
- //--------------------------------
- class Blog extends CentralizedComposite {
- private $closed = false;
- public function open_blog() {
- $this->closed = false;
- $this->mediator->opened($this);
- }
- public function close_blog() {
- $this->closed = true;
- $this->mediator->closed($this);
- }
- public function draw() {
- echo 'blog '.$this->element_id.' '.($this->closed ? 'closed' : 'open').'<br/>';
- foreach ($this->getAll() as $post) {
- $post->draw();
- }
- }
- }
- //--------------------------------
- class Post extends CentralizedComposite {
- private $closed = false;
- public function open_post() {
- $this->closed = false;
- $this->mediator->opened($this);
- }
- public function close_post() {
- $this->closed = true;
- $this->mediator->closed($this);
- }
- public function draw() {
- echo str_repeat(' ',3).'post '.$this->element_id.' '.($this->closed ? 'closed' : 'open').'<br/>';
- foreach ($this->getAll() as $comment) {
- $comment->draw();
- }
- }
- }
- //--------------------------------
- class Comment {
- private $closed = false;
- public function open_comment() {
- $this->closed = false;
- }
- public function close_comment() {
- $this->closed = true;
- }
- public function draw() {
- echo str_repeat(' ',6).'comment '.($this->closed ? 'closed' : 'open').'<br/>';
- }
- }
- //--------------------------------
- echo 'Begin testing MEDIATOR pattern<br/>';
- $mediator = new BlogMediator();
- $blog = new Blog(8, $mediator);
- $post4 = new Post(4, $mediator);
- $post5 = new Post(5, $mediator);
- $comment45 = new Comment(45, $mediator);
- $comment46 = new Comment(46, $mediator);
- $comment55 = new Comment(55, $mediator);
- $comment56 = new Comment(56, $mediator);
- $post4->add($comment45)->add($comment46);
- $post5->add($comment55)->add($comment56);
- $blog->add($post4)->add($post5);
- $blog->draw();
- echo '<br/>';
- $post4->close_post();
- $comment55->close_comment();
- $blog->draw();
- echo '<br/>';
- $blog->close_blog();
- $blog->draw();
- /*----------------------------------------------------------------
- Memento
- ----------------------------------------------------------------*/
- /*----------------------------------------------------------------
- Visitor
- ----------------------------------------------------------------*/
- interface IVisitee {
- function accept(IVisitor $visitor_in);
- }
- interface IVisitor {
- function visit_post (Post $visitee);
- function visit_comment (Comment $visitee);
- }
- //--------------------------------
- class Post implements IVisitee {
- private $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- public function get_postid() {
- return $this->post_id;
- }
- public function accept(IVisitor $visitor_in) {
- $visitor_in->visit_post($this);
- }
- }
- //--------------------------------
- class Comment implements IVisitee {
- private $comment_id;
- public function __construct($id) {
- $this->comment_id = $id;
- }
- public function get_commentid() {
- return $this->comment_id;
- }
- public function accept(IVisitor $visitor_in) {
- $visitor_in->visit_comment($this);
- }
- }
- //--------------------------------
- class plainVisitor implements IVisitor {
- public function visit_post(Post $visitee) {
- echo 'post: '.$visitee->get_postid().'<br/>';
- }
- public function visit_comment(Comment $visitee) {
- echo 'comment: '.$visitee->get_commentid().'<br/>';
- }
- }
- //--------------------------------
- class fancyVisitor implements IVisitor {
- public function visit_post(Post $visitee) {
- echo '<b>post: '.$visitee->get_postid().'</b><br/>';
- }
- public function visit_comment(Comment $visitee) {
- echo '<b>comment: '.$visitee->get_commentid().'</b><br/>';
- }
- }
- //--------------------------------
- echo 'Begin testing VISITOR pattern<br/>';
- $post = new Post(4);
- $comment = new Comment(5);
- $plain_visitor = new plainVisitor();
- $fancy_visitor = new fancyVisitor();
- echo 'test plain visitor<br/>';
- $post->accept($plain_visitor);
- $comment->accept($plain_visitor);
- echo 'test fancy visitor<br/>';
- $post->accept($fancy_visitor);
- $comment->accept($fancy_visitor);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement