Ver Mensaje Individual
  #5 (permalink)  
Antiguo 18/07/2011, 11:17
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Patron decorador

Recuerda el punto clave, los Decoradores son para agregar/decorar un objeto base y/o modificar su resultado de la clase base. Esto es que los decoradores no deben de funcionar por si solos, y solo es un agregado a la clase, revisa este ejemplo que diseñe:

Código PHP:
Ver original
  1. $html = new Html();
  2. $html->addTag(new Link(new Img('foto.jpg'), 'http://www.php.net/'));
  3. $html->addTag(new Bold(new Node('Test')));
  4. echo htmlentities($html);

En este caso hay dos nodos base, Img, y Node, que son nodos base que no pueden decorar a otro nodo (no puedes hacer <img><algo></img>).

Las clases que use son las siguientes:
Código PHP:
Ver original
  1. // Base Tag Class
  2. abstract class Tag
  3. {
  4.     abstract public function render();
  5.    
  6.     public function __toString()
  7.     {
  8.         return $this->render();
  9.     }
  10. }
  11.  
  12. // Base Decorator Class
  13. abstract class TagDecorator extends Tag
  14. {
  15.     protected $_tag;
  16.    
  17.     public function __construct(Tag $tag)
  18.     {
  19.         $this->_tag = $tag;
  20.     }
  21.    
  22.     public function getTag()
  23.     {
  24.         return $this->_tag;
  25.     }
  26. }
  27.  
  28. class Html
  29. {
  30.     private $_tags = array();
  31.     public function __construct(){}
  32.    
  33.     public function addTag(Tag $tag)
  34.     {
  35.         $this->_tags[] = $tag;
  36.     }
  37.    
  38.     public function renderHtml()
  39.     {
  40.         $strOutput = '';
  41.         foreach ($this->_tags as $Tag) {
  42.             $strOutput = (string) $Tag;
  43.         }
  44.        
  45.         return $strOutput;
  46.     }
  47.    
  48.     public function __toString()
  49.     {
  50.         return $this->renderHtml();
  51.     }
  52. }
  53.  
  54. /** Base Clases **/
  55. class Node extends Tag
  56. {
  57.     private $_text;
  58.    
  59.     public function __construct($text)
  60.     {
  61.         $this->_text = $text;
  62.     }
  63.    
  64.     public function render()
  65.     {
  66.         return $this->_text;
  67.     }
  68. }
  69.  
  70. class Img extends Tag
  71. {
  72.     private $_path;
  73.     private $_border;
  74.    
  75.     public function __construct($path, $border = 0)
  76.     {
  77.         $this->_path = $path;
  78.         $this->_border = $border;
  79.     }
  80.    
  81.     public function render()
  82.     {
  83.         return '<img src="' . $this->_path . '" border="'.$this->_border.'" />');
  84.     }
  85. }
  86.  
  87. /** Decorators **/
  88.  
  89. class Link extends TagDecorator
  90. {  
  91.     private $_url;
  92.    
  93.     public function __construct(Tag $text, $url)
  94.     {
  95.         parent::__construct($text);
  96.         $this->setUrl($url);
  97.     }
  98.    
  99.     public function setUrl($url)
  100.     {
  101.         $this->_url = $url;
  102.     }
  103.    
  104.     public function render()
  105.     {
  106.         return '<a href="' . $this->_url . '">' . $this->getTag() . '</a>';
  107.     }
  108. }
  109.  
  110. class Bold extends TagDecorator
  111. {  
  112.     public function render()
  113.     {
  114.         return '<b>' . $this->getTag() . '</b>';
  115.     }
  116. }
  117.  
  118. class Italic extends TagDecorator
  119. {
  120.     public function render()
  121.     {
  122.         return '<i>' . $this->getTag() . '</i>';
  123.     }
  124. }

Saludos.

Última edición por GatorV; 18/07/2011 a las 11:26