mihaild

Untitled

Jan 15th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.97 KB | None | 0 0
  1. /********************************************************************
  2. Model-View-Controller implementation according to POSA
  3. (Pattern-Oriented Software Architecture
  4.   http://www.hillside.net/patterns/books/Siemens/book.html)
  5. ********************************************************************/
  6.  
  7. class HelloWorldController {
  8.     private $model;
  9.     function __construct($model) {
  10.         $this->model = $model;
  11.     }
  12.  
  13.     function handleEvent($args) {
  14.         $this->model->setStrategy($args[2]);
  15.         $this->model->addText($args[1]);
  16.     }
  17. }
  18.  
  19.  
  20. class HelloWorldModel {
  21.     private $text;
  22.     private $observers = array();
  23.     private $strategy;
  24.  
  25.     function attach($observer) {
  26.         $this->observers[] = $observer;
  27.     }
  28.  
  29.     function getData() {
  30.         $facade = new HelloWorldFacade($this->strategy);
  31.         return $facade->getHelloWorld().$this->text."\n";
  32.     }
  33.  
  34.     function addText($text='') {
  35.         $this->text = $text;
  36.         $this->notify();
  37.     }
  38.  
  39.     function setStrategy($strategy) {
  40.         $this->strategy = $strategy;
  41.     }
  42.  
  43.     function notify() {
  44.         foreach ($this->observers as $observer) {
  45.             $observer->update();
  46.         }
  47.     }
  48. }
  49.  
  50. class HelloWorldView {
  51.     private $model;
  52.  
  53.     function initialize($model) {
  54.         $this->model = $model;
  55.         $model->attach($this);
  56.         return $this->makeController();
  57.     }
  58.  
  59.     function makeController() {
  60.         return new HelloWorldController($this->model);
  61.     }
  62.  
  63.     function update() {
  64.         $this->display();
  65.     }
  66.  
  67.     function display() {
  68.         echo $this->model->getData();
  69.     }
  70. }
  71.  
  72.  
  73. /*********************************************************************
  74. "Business logic"
  75. ********************************************************************/
  76.  
  77. class HelloWorld {
  78.    function execute() {
  79.        return "Hello world";
  80.    }
  81. }
  82.  
  83. class HelloWorldDecorator {
  84.    private $helloworld;
  85.    function __construct($helloworld) {
  86.        $this->helloworld = $helloworld;
  87.    }
  88.  
  89.    function execute() {
  90.        return $this->helloworld->execute();
  91.    }
  92. }
  93.  
  94. abstract class HelloWorldEmphasisStrategy {
  95.     abstract function emphasize($string);
  96. }
  97.  
  98. class HelloWorldBangEmphasisStrategy extends HelloWorldEmphasisStrategy {
  99.     function emphasize($string) {
  100.        return $string."!";
  101.     }
  102. }
  103.  
  104. class HelloWorldRepetitionEmphasisStrategy extends HelloWorldEmphasisStrategy {
  105.     function emphasize($string) {
  106.        return $string." and ".$string." again";
  107.     }
  108. }
  109.  
  110. class HelloWorldEmphasizer extends HelloWorldDecorator {
  111.    private $strategy;
  112.    function HelloWorldEmphasizer($helloworld,$strategy) {
  113.        $this->strategy = $strategy;
  114.        parent::__construct($helloworld);
  115.    }
  116.  
  117.    function execute() {
  118.        $string = parent::execute();
  119.        return $this->strategy->emphasize($string);
  120.    }
  121. }
  122.  
  123. class HelloWorldStrategyFactory {
  124.     static function make($type) {
  125.         if ($type == 'repetition') return self::makeRepetitionStrategy();
  126.         return self::makeBangStrategy();
  127.     }
  128.  
  129.     static function makeBangStrategy() {
  130.         return new HelloWorldBangEmphasisStrategy;
  131.     }
  132.     static function makeRepetitionStrategy() {
  133.         return new HelloWorldRepetitionEmphasisStrategy;
  134.     }
  135. }
  136.  
  137. class HelloWorldFormatter extends HelloWorldDecorator {
  138.    function execute() {
  139.        $string = parent::execute();
  140.        return $string."\n";
  141.    }
  142. }
  143.  
  144. class HelloWorldFacade {
  145.     private $strategy;
  146.     function __construct($strategyType) {
  147.         $this->strategy = HelloWorldStrategyFactory::make($strategyType);
  148.     }
  149.  
  150.     function getHelloWorld() {
  151.         $formatter = new HelloWorldFormatter(
  152.                 new HelloWorldEmphasizer(
  153.                     new HelloWorld,$this->strategy));
  154.         return $formatter->execute();
  155.     }
  156. }
  157.  
  158. $model = new HelloWorldModel;
  159. $view = new HelloWorldView;
  160. $controller = $view->initialize($model);
  161. $controller->handleEvent($_SERVER['argv']);
Advertisement
Add Comment
Please, Sign In to add comment