Advertisement
Guest User

оверинжиниринг

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