Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Prosta aplikacja oparta na MVC
sztosz
post
Post #1





Grupa: Zarejestrowani
Postów: 866
Pomógł: 32
Dołączył: 2.06.2004
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


Pamiętam ze ktoś kiedys dał linka do aplikacji typu "Hello World" opartej na MVC. Bedę wdzięczny jeżeli ktoś mi pomoże znaleźć to, albo poda jakiegoś linka do kompletnej, niewielkiej (chodzi mi o zobaczenie MVC w php w praktyce). Szukam od rana tego, ale znajduję tylkosamą teorię, a tą jako tako znam, i potrzeba mi tylko konkretnego rozwiązania, aby sprawdzić swoje założenia i domysły.

Jeżeli ktoś będzie łaskaw podać mi linka do tematu z tym linkiem do "Hello World" opartego na MVC, to można od razu ten temat zamknąć/w kosz/unicestwić, oczywiście jeżeli tylko ten link do mnie dojdzie (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
mike
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

Ostrzeżenie: (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. ?>


LOL,

sorki ale nie mogłem się powstrzymać (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Closed TopicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 5.10.2025 - 07:36