Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [mvc] Pare pytan, lancuchy akcji etc.
Helios
post
Post #1





Grupa: Zarejestrowani
Postów: 24
Pomógł: 0
Dołączył: 8.07.2006

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


Tak sobie od kilku dni czytam ksiazeczke na temat programowania obiektowego i chce zrozumiec wzorzec MVC. Sklecilem taki kod i chce zebyscie go zweryfikowali czy moje rozumowanie idzie w dobra strone.
Bez obslugi bledow, bez pojedynczych instancji, prosty kod zeby sie przekonac czy dobrze rozumuje itd. Narazie sie bawie...
Zerknijcie okiem i napiszcie cos od siebie. Dzieki z gory za wszelkie uwagi!

  1. <?php
  2.  
  3. /**
  4. * Interfejs kontrolera - kazdy niech ma metode ustawiania widoku
  5. */
  6. interface Controller {
  7. public function setView($viewName);
  8. }
  9.  
  10. /**
  11. * Interfejs widoku - kazdy niech ma metode wyswietlania
  12. */
  13. interface View {
  14. public function display();
  15. }
  16.  
  17. /**
  18. * Klasa widoku produktu z zaimplementowanym interfejsem widoku
  19. */
  20. class ProductView implements View {
  21.  
  22. private $vars = array();
  23.  
  24. public function setVar($varName, $varValue = false){
  25. $this->vars[$varName] = $varValue;
  26. }
  27.  
  28. public function display(){
  29. foreach($this->vars AS $varName => $varValue){
  30. print "[{$varName}] => {$varValue}\n";
  31. }
  32. }
  33.  
  34. }
  35.  
  36.  
  37. /**
  38. * Klasa pojedynczego produktu
  39. */
  40. class Product {
  41.  
  42. protected $title;
  43. protected $price;
  44.  
  45. public function setTitle($title){
  46. $this->title = $title;
  47. }
  48.  
  49. public function setPrice($price){
  50. $this->price = $price;
  51. }
  52.  
  53. public function getTitle(){
  54. return $this->title;
  55. }
  56.  
  57. public function getPrice(){
  58. return $this->price;
  59. }
  60.  
  61. }
  62.  
  63. /**
  64. * Klasa kontrolera produktow korzysta z klasy Product i implementuje interfejs k
    ontrolera
  65. * Wywolanie akcji addProduct powoduje ustawienie title, price, widoku ProductVie
    w i wyswietlenie danych
  66. */
  67. class ProductController extends Product implements Controller {
  68.  
  69. private $viewObj;
  70.  
  71. public function addProduct($title, $price){
  72. $this->setTitle($title);
  73. $this->setPrice($price);
  74. $this->setView('ProductView');
  75. $this->viewObj->setVar($this->getTitle(), $this->getPrice());
  76. $this->execView();
  77. }
  78.  
  79. public function setView($viewName){
  80. $this->viewObj = new $viewName;
  81. }
  82.  
  83. public function execView(){
  84. $this->viewObj->display();
  85. }
  86.  
  87. }
  88.  
  89. /**
  90. * Tu powinien byc jakis front controller ktory wywola akurat kontroler produktow
     ale do tego jeszcze nie doszedlem
  91. */
  92. $o = new ProductController;
  93. $o->addProduct('Nazwa 1', '44.99');
  94.  
  95. ?>


Ten post edytował Helios 13.08.2007, 17:54:47
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 18:56