Advertisement
terorama

Patterns / Creational

Jun 9th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.59 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /*----------------------------------------------------------------
  5.                      abstract factory
  6. ----------------------------------------------------------------*/
  7.  
  8. abstract class AbstractPostFactory {
  9.    
  10.    abstract function makePost($id);
  11.    abstract function makeComment($id);
  12. }
  13. //------------------------------------
  14. abstract class AbstractPost {
  15.  
  16.    protected $post_id;
  17.    
  18.    public function __construct($id=0) {
  19.       $this->post_id = $id;
  20.    }  
  21.    abstract function drawPost();
  22. }
  23. //------------------------------------
  24. abstract class AbstractComment {
  25.  
  26.    protected $comment_id;
  27.    
  28.    public function __construct($id=0) {
  29.       $this->comment_id = $id;
  30.    }
  31.    abstract function drawComment();
  32. }
  33.  
  34. //------------------------------------
  35. class SimplePostFactory extends AbstractPostFactory {
  36.  
  37.    public function makePost($id) {
  38.       return new SimplePost($id);
  39.    }
  40.    public function makeComment($id) {
  41.       return new SimpleComment($id);
  42.    }
  43. }
  44. //------------------------------------
  45. class FancyPostFactory extends AbstractPostFactory {
  46.  
  47.    public function makePost($id) {
  48.       return new FancyPost($id);
  49.    }
  50.    
  51.    public function makeComment($id) {
  52.       return new FancyComment($id);
  53.    }
  54. }
  55. //------------------------------------
  56. class SimplePost extends AbstractPost {
  57.    public function drawPost() {
  58.       echo 'post '.$this->post_id."<br/>\n";
  59.    }
  60. }
  61. class FancyPost extends AbstractPost {
  62.    public function drawPost() {
  63.       echo '<b>post '.$this->post_id.'</b>'."<br/>\n";
  64.    }
  65. }
  66. //------------------------------------
  67. class SimpleComment extends AbstractComment {
  68.    public function drawComment() {
  69.       echo 'comment '.$this->comment_id."<br/>\n";
  70.    }
  71. }
  72. class FancyComment extends AbstractComment {
  73.    public function drawComment() {
  74.       echo '<b>comment '.$this->comment_id.'</b>'."<br/>\n";
  75.    }
  76. }
  77. //------------------------------------
  78. echo "Begin testing ABSTRACT FACTORY pattern<br/>\n";
  79. echo "Testing SimpleFactory<br/>\n";
  80.  
  81. $inst = new SimplePostFactory();
  82. testFactory($inst);
  83.  
  84. echo "Testing FancyFactory<br/>\n";
  85.  
  86. $inst = new FancyPostFactory();
  87. testFactory($inst);
  88.  
  89. //------------------------------------
  90. function testFactory($inst) {
  91.    $zpost = $inst->makePost(4);
  92.    $zpost->drawPost();
  93.    
  94.    $zcomment = $inst->makeComment(4);
  95.    $zcomment->drawComment();
  96. }
  97.  
  98.  
  99. /*----------------------------------------------------------------
  100.                        builder
  101. ----------------------------------------------------------------*/
  102.  
  103. abstract class AbstractPostBuilder {
  104.  
  105.    protected $post_id;
  106.    
  107.    public function __construct($id) {
  108.       $this->post_id = $id;
  109.    }
  110.  
  111. }
  112.  
  113. //---------------------------------------------
  114. abstract class AbstractDirector {
  115.  
  116.    protected $builder;
  117.  
  118.    abstract function makePost();
  119.  
  120.    public function setBuilder(AbstractPostBuilder $builder_in) {
  121.    
  122.       $this->builder = $builder_in;
  123.    }
  124. }
  125. //---------------------------------------------
  126.  
  127. class SimplePostBuilder extends AbstractPostBuilder {
  128.    
  129.    public function setTitle() {
  130.       echo 'post '.$this->post_id."<br/>\n";
  131.    }
  132.    public function setExcerpt() {
  133.       echo "Lorem Ipsum<br/>\n";
  134.    }
  135.    public function setAuthor() {
  136.       echo "Test author<br/>\n";
  137.    }
  138. }
  139. //---------------------------------------------
  140. class FancyPostBuilder extends AbstractPostBuilder {
  141.  
  142.    public function setTitle() {
  143.       echo '<b>post '.$this->post_id."</b><br/>\n";
  144.    }
  145.    public function setExcerpt() {
  146.       echo "<b>Lorem Ipsum</b><br/>\n";
  147.    }
  148.    public function setAuthor() {
  149.       echo "<b>Test author</b><br/>\n";
  150.    }
  151. }
  152. //---------------------------------------------
  153. class PostDirector extends AbstractDirector {
  154.    public function makePost() {
  155.       $this->builder->setTitle();
  156.       $this->builder->setExcerpt();
  157.       $this->builder->setAuthor();
  158.    }
  159. }
  160.  
  161. echo "Begin testing BUILDER pattern<br/>\n";
  162.  
  163. $simple_builder = new SimplePostBuilder(4);
  164. $fancy_builder = new FancyPostBuilder(4);
  165.  
  166. $director = new PostDirector();
  167.  
  168. echo "Testing SimplePostBuilder<br/>\n";
  169.  
  170. $director->setBuilder($simple_builder);
  171. $director->makePost();
  172.  
  173. echo "Testing FancyPostBuilder<br/>\n";
  174.  
  175. $director->setBuilder($fancy_builder);
  176. $director->makePost();
  177.  
  178.  
  179. /*----------------------------------------------------------------
  180.                        Factory Method
  181. ----------------------------------------------------------------*/
  182. abstract class AbstractFactoryMethod {
  183.  
  184.    abstract function makePost($id, $type);
  185. }
  186. //---------------------------------------------
  187. abstract class AbstractPost {
  188.  
  189.    protected $post_id;
  190.    
  191.    public function __construct($id) {
  192.       $this->post_id = $id;
  193.    }
  194.    
  195.    abstract function draw();
  196. }
  197. //---------------------------------------------
  198. class SimplePost extends AbstractPost {
  199.    
  200.    public function draw() {
  201.       echo "post {$this->post_id}<br/>\n";
  202.    }
  203. }
  204. //---------------------------------------------
  205. class FancyPost extends AbstractPost {
  206.    
  207.    public function draw() {
  208.       echo "<b>post {$this->post_id}</b><br/>\n";
  209.    }
  210. }
  211. //---------------------------------------------
  212. class FactoryMethod extends AbstractFactoryMethod {
  213.  
  214.    public function makePost($id, $type) {
  215.    
  216.       switch ($type) {
  217.          case 'Simple':  $zpost = new SimplePost($id); break;
  218.          case 'Fancy': $zpost = new FancyPost($id); break;
  219.       }
  220.       return $zpost;
  221.      
  222.    }
  223. }
  224.  
  225. echo "Begin testing FACTORY METHOD pattern<br/>\n";
  226.  
  227. $factory_method = new FactoryMethod();
  228.  
  229. echo "Testing SimplePost<br/>\n";
  230.  
  231. $post = $factory_method->makePost(4,'Simple');
  232. $post->draw();
  233.  
  234. echo "Testing FancyPost<br/>\n";
  235.  
  236. $post = $factory_method->makePost(4,'Fancy');
  237. $post->draw();
  238.  
  239.  
  240. /*----------------------------------------------------------------
  241.                        Prototype
  242. ----------------------------------------------------------------*/
  243.  
  244. abstract class PostPrototype {
  245.    
  246.    protected $post_id;
  247.    
  248.    public function __construct() {
  249.      
  250.    }
  251.    public function zclone($id) {
  252.    
  253.       $el = clone $this;
  254.       $el->post_id = $id;
  255.       return $el;
  256.    }
  257.    abstract function draw();
  258. }
  259.  
  260. //---------------------------------------------
  261. class SimplePost extends PostPrototype {
  262.  
  263.    public function draw() {
  264.       echo "post {$this->post_id} <br/>\n";
  265.    }
  266. }
  267. //---------------------------------------------
  268. class FancyPost extends PostPrototype {
  269.    
  270.    public function draw() {
  271.       echo "<b>post {$this->post_id}</b><br/>\n";
  272.    }
  273. }
  274.  
  275. //---------------------------------------------
  276. echo "Begin testing PROTOTYPE pattern<br/>\n";
  277.  
  278. $simple_proto = new SimplePost();
  279. $fancy_proto = new FancyPost();
  280.  
  281. echo "Testing SimplePost<br/>\n";
  282.  
  283. $post = $simple_proto->zclone(4);
  284. $post->draw();
  285.  
  286. echo "Testing FancyPost<br/>\n";
  287.  
  288. $post = clone $fancy_proto->zclone(4);
  289. $post->draw();
  290.  
  291. /*----------------------------------------------------------------
  292.                        Singleton
  293. ----------------------------------------------------------------*/
  294.  
  295. class PostSingleton {
  296.  
  297.    private $post_id;
  298.    private static $zpost=null;
  299.    
  300.    private function __construct() {
  301.    }
  302.    
  303.    public static function getPost($id) {
  304.       if (self::$zpost == null) {
  305.          self::$zpost = new self();
  306.       }
  307.      
  308.       self::$zpost->post_id = $id;
  309.       return self::$zpost;
  310.    }
  311.    
  312.    public function draw() {
  313.       echo "post {$this->post_id}<br/>\n";
  314.    }
  315.    
  316. }
  317.  
  318. echo "Begin testing SINGLETON pattern<br/>\n";
  319.  
  320. $post_4  = PostSingleton::getPost(4);
  321. $post_4->draw();
  322.  
  323. $post_5 = PostSingleton::getPost(5);
  324. $post_5->draw();
  325. $post_4->draw();
  326.  
  327.  
  328. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement