Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*----------------------------------------------------------------
- abstract factory
- ----------------------------------------------------------------*/
- abstract class AbstractPostFactory {
- abstract function makePost($id);
- abstract function makeComment($id);
- }
- //------------------------------------
- abstract class AbstractPost {
- protected $post_id;
- public function __construct($id=0) {
- $this->post_id = $id;
- }
- abstract function drawPost();
- }
- //------------------------------------
- abstract class AbstractComment {
- protected $comment_id;
- public function __construct($id=0) {
- $this->comment_id = $id;
- }
- abstract function drawComment();
- }
- //------------------------------------
- class SimplePostFactory extends AbstractPostFactory {
- public function makePost($id) {
- return new SimplePost($id);
- }
- public function makeComment($id) {
- return new SimpleComment($id);
- }
- }
- //------------------------------------
- class FancyPostFactory extends AbstractPostFactory {
- public function makePost($id) {
- return new FancyPost($id);
- }
- public function makeComment($id) {
- return new FancyComment($id);
- }
- }
- //------------------------------------
- class SimplePost extends AbstractPost {
- public function drawPost() {
- echo 'post '.$this->post_id."<br/>\n";
- }
- }
- class FancyPost extends AbstractPost {
- public function drawPost() {
- echo '<b>post '.$this->post_id.'</b>'."<br/>\n";
- }
- }
- //------------------------------------
- class SimpleComment extends AbstractComment {
- public function drawComment() {
- echo 'comment '.$this->comment_id."<br/>\n";
- }
- }
- class FancyComment extends AbstractComment {
- public function drawComment() {
- echo '<b>comment '.$this->comment_id.'</b>'."<br/>\n";
- }
- }
- //------------------------------------
- echo "Begin testing ABSTRACT FACTORY pattern<br/>\n";
- echo "Testing SimpleFactory<br/>\n";
- $inst = new SimplePostFactory();
- testFactory($inst);
- echo "Testing FancyFactory<br/>\n";
- $inst = new FancyPostFactory();
- testFactory($inst);
- //------------------------------------
- function testFactory($inst) {
- $zpost = $inst->makePost(4);
- $zpost->drawPost();
- $zcomment = $inst->makeComment(4);
- $zcomment->drawComment();
- }
- /*----------------------------------------------------------------
- builder
- ----------------------------------------------------------------*/
- abstract class AbstractPostBuilder {
- protected $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- }
- //---------------------------------------------
- abstract class AbstractDirector {
- protected $builder;
- abstract function makePost();
- public function setBuilder(AbstractPostBuilder $builder_in) {
- $this->builder = $builder_in;
- }
- }
- //---------------------------------------------
- class SimplePostBuilder extends AbstractPostBuilder {
- public function setTitle() {
- echo 'post '.$this->post_id."<br/>\n";
- }
- public function setExcerpt() {
- echo "Lorem Ipsum<br/>\n";
- }
- public function setAuthor() {
- echo "Test author<br/>\n";
- }
- }
- //---------------------------------------------
- class FancyPostBuilder extends AbstractPostBuilder {
- public function setTitle() {
- echo '<b>post '.$this->post_id."</b><br/>\n";
- }
- public function setExcerpt() {
- echo "<b>Lorem Ipsum</b><br/>\n";
- }
- public function setAuthor() {
- echo "<b>Test author</b><br/>\n";
- }
- }
- //---------------------------------------------
- class PostDirector extends AbstractDirector {
- public function makePost() {
- $this->builder->setTitle();
- $this->builder->setExcerpt();
- $this->builder->setAuthor();
- }
- }
- echo "Begin testing BUILDER pattern<br/>\n";
- $simple_builder = new SimplePostBuilder(4);
- $fancy_builder = new FancyPostBuilder(4);
- $director = new PostDirector();
- echo "Testing SimplePostBuilder<br/>\n";
- $director->setBuilder($simple_builder);
- $director->makePost();
- echo "Testing FancyPostBuilder<br/>\n";
- $director->setBuilder($fancy_builder);
- $director->makePost();
- /*----------------------------------------------------------------
- Factory Method
- ----------------------------------------------------------------*/
- abstract class AbstractFactoryMethod {
- abstract function makePost($id, $type);
- }
- //---------------------------------------------
- abstract class AbstractPost {
- protected $post_id;
- public function __construct($id) {
- $this->post_id = $id;
- }
- abstract function draw();
- }
- //---------------------------------------------
- class SimplePost extends AbstractPost {
- public function draw() {
- echo "post {$this->post_id}<br/>\n";
- }
- }
- //---------------------------------------------
- class FancyPost extends AbstractPost {
- public function draw() {
- echo "<b>post {$this->post_id}</b><br/>\n";
- }
- }
- //---------------------------------------------
- class FactoryMethod extends AbstractFactoryMethod {
- public function makePost($id, $type) {
- switch ($type) {
- case 'Simple': $zpost = new SimplePost($id); break;
- case 'Fancy': $zpost = new FancyPost($id); break;
- }
- return $zpost;
- }
- }
- echo "Begin testing FACTORY METHOD pattern<br/>\n";
- $factory_method = new FactoryMethod();
- echo "Testing SimplePost<br/>\n";
- $post = $factory_method->makePost(4,'Simple');
- $post->draw();
- echo "Testing FancyPost<br/>\n";
- $post = $factory_method->makePost(4,'Fancy');
- $post->draw();
- /*----------------------------------------------------------------
- Prototype
- ----------------------------------------------------------------*/
- abstract class PostPrototype {
- protected $post_id;
- public function __construct() {
- }
- public function zclone($id) {
- $el = clone $this;
- $el->post_id = $id;
- return $el;
- }
- abstract function draw();
- }
- //---------------------------------------------
- class SimplePost extends PostPrototype {
- public function draw() {
- echo "post {$this->post_id} <br/>\n";
- }
- }
- //---------------------------------------------
- class FancyPost extends PostPrototype {
- public function draw() {
- echo "<b>post {$this->post_id}</b><br/>\n";
- }
- }
- //---------------------------------------------
- echo "Begin testing PROTOTYPE pattern<br/>\n";
- $simple_proto = new SimplePost();
- $fancy_proto = new FancyPost();
- echo "Testing SimplePost<br/>\n";
- $post = $simple_proto->zclone(4);
- $post->draw();
- echo "Testing FancyPost<br/>\n";
- $post = clone $fancy_proto->zclone(4);
- $post->draw();
- /*----------------------------------------------------------------
- Singleton
- ----------------------------------------------------------------*/
- class PostSingleton {
- private $post_id;
- private static $zpost=null;
- private function __construct() {
- }
- public static function getPost($id) {
- if (self::$zpost == null) {
- self::$zpost = new self();
- }
- self::$zpost->post_id = $id;
- return self::$zpost;
- }
- public function draw() {
- echo "post {$this->post_id}<br/>\n";
- }
- }
- echo "Begin testing SINGLETON pattern<br/>\n";
- $post_4 = PostSingleton::getPost(4);
- $post_4->draw();
- $post_5 = PostSingleton::getPost(5);
- $post_5->draw();
- $post_4->draw();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement