Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [OOP] Relacja Model <-> Widok, Dość ciekawy sposób.
matix
post 23.07.2007, 14:47:19
Post #1





Grupa: Zarejestrowani
Postów: 278
Pomógł: 10
Dołączył: 13.02.2007
Skąd: Rybnik

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


Witam,
Chciałem Wam pokazać mój sposób na relację Model <-> Widok. Nie jest najtrudniejsza, ale działa jak powinna.
Oczywiście jest to tylko prototyp i nie ma związku z tym, z czym korzystam, tworząc swoje strony www.

  1. <?
  2. // Singleton Interface
  3. Interface Singleton {
  4. public function singleton();
  5. }
  6.  
  7. // Observ Interface
  8. Interface Observ {
  9. public function Update();
  10. }
  11.  
  12. // ObState Interface
  13. Interface ObState {
  14. public function notify();
  15. public function register($what);
  16. }
  17.  
  18. // View Class
  19. class View implements Singleton, Observ
  20. {
  21. public static $singleton;
  22. public static $data;
  23.  
  24. public function singleton()
  25. {
  26. if (self::$singleton == null)
  27. self::$singleton = new self();
  28.  
  29. return self::$singleton;
  30. }
  31.  
  32. public function update()
  33. {
  34. $aParametrs = func_get_args();
  35.  
  36. self::$data = $aParametrs[0];
  37. }
  38.  
  39. public function __get($a)
  40. {
  41. return self::$data -> $a;
  42. }
  43. }
  44.  
  45. // Model Class
  46. class Model implements Singleton, ObState
  47. {
  48. public static $singleton;
  49. protected static $observs;
  50. public $data;
  51.  
  52. public function singleton()
  53. {
  54. if (self::$singleton == null)
  55. self::$singleton = new self();
  56.  
  57. return self::$singleton;
  58. }
  59.  
  60. public function register($what) 
  61. {
  62. self::$observs[] = $what;
  63. }
  64.  
  65. public function notify()
  66. {
  67. foreach (self::$observs as $observ)
  68. {
  69. $observ -> update($this -> data);
  70. }
  71. }
  72. }
  73.  
  74. // SomeModel Class
  75. Class SomeModel extends Model {
  76. public function doSomethink()
  77. {
  78. $this -> data -> shit = 'matix';
  79. $this -> data -> dbResult = 'select * from blabla';
  80. #$this -> data -> jeszcze -> jeden -> shit = 'ble';
  81.  
  82. $this -> notify();
  83. }
  84. }
  85.  
  86. //SomeView Class
  87. Class SomeView extends View {
  88. public function getSomeVars()
  89. {
  90. echo $this -> shit;
  91. }
  92. }
  93.  
  94. // Running ...
  95.  
  96. $view = View::singleton();
  97. $model = Model::singleton();
  98.  
  99. $model -> register($view);
  100.  
  101. $sm = new SomeModel;
  102. $sm -> doSomethink();
  103.  
  104. $sv = new SomeView;
  105. $sv -> getSomeVars();
  106. ?>


Pomijam tutaj Kontroller, bo on mnie tutaj nie interesuje.
Myślicie, że taka implementacja w frameworku mogłaby być ciekawa?
Plusem tego jest to, że dane są przechowywane w taki sposób:

  1. <?php
  2. $this -> data -> NAZWA = 'wartosc';
  3. $this -> data -> imie = 'mateusz';
  4. $this -> data -> wiek = '15 lat';
  5. $this -> data -> time = time();
  6. $this -> data -> adresy -> domowe -> wszystkich -> uzytkownikow -> serwisu -> o -> dragonballu;
  7. ?>


Dane zostają następnie przesłane do Widoku poprzez Wzorzec Oberwator, i są dostępne z każdego miejsca.

W widoku odwoływanie się do danych jest w ten sam sposób, tyko pomijając '- > data ->', czyli:

  1. <?php
  2. echo $this -> imie;
  3. echo $this -> wiek;
  4. echo $this -> time;
  5. echo 4this -> adresy -> domowe -> wszystkich -> uzytkownikow -> serwisu -> o -> dragonballu;
  6. ?>


Co wy na to smile.gif ?


--------------------
Nawet, jeżeli nie jesteś zainteresowany usługami IT ani outsourcingiem, a Twoją pasją jest programowanie - zobacz naszą stronę. Piszemy dużo fajnych use-caseów, jak podchodzimy do tematu programowania dla naszych klientów. A tak na co dzień tworzymy budujemy mvp oraz tworzymy platformę b2b.
Go to the top of the page
+Quote Post
UDAT
post 23.07.2007, 14:58:02
Post #2





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


Moja krytyka nie będzie konstruktywna:
publiczne właściwości -> błe
użycie wzorca observer -> nie uzasadnione

Ogólnie mi się nie podoba, poza tym funkcja Update jest jakaś chora. Poza tym czemu nie użyłeś SplSubject i SplObserver z SPL'a??
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 25.07.2025 - 07:54