Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/07/2011, 14:00
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Patron decorador

Me pregunto si el patron decorator esta o no bien aplicado......

Código PHP:
<?php
class html {
  protected 
$html;  
   
  function 
__construct($html){
    
$this->setHtml($html);
  }  
   
  function 
__tostring(){
    return 
$this->html;
  }
  
  function 
setHtml($html){
    
$this->html =$html;
  }
#

/* decorator */
abstract class tag extends html 
  protected 
$obj;
  
  function 
__construct ($obj=null){
    
$this->obj $obj;
  }   

#  
  
class link extends tag {
  private 
$uri;

  function 
__construct ($x=null){
    if (
$x instanceof tag){
      
parent::__construct($x);
    }else{
      
$y = new html($x);
      
parent::__construct($x);
      return 
$y;
    }
  }
  
  function 
setUri($uri){
    
$this->uri $uri;
  }

  function 
linkear (){
    
$this->setHtml ("<a href='{$this->uri}'>{$this->obj}</a>");
  }
  

  
 
class strong extends tag {

  function 
render(){
    
$this->setHtml ('<strong>'.$this->obj.'</strong>');
  }  
  
#

class italic extends tag {

  function 
render(){
    
$this->setHtml ('<i>'.$this->obj.'</i>');
  }  
  
#

class img extends html {
  private 
$img;
  
  function 
__construct($img){
    
$this->img $img;
    
$this->render();
  }  
  
  private function 
render(){
    
$this->setHtml ("<img src='{$this->img}' />");
  }
#

/* Creo HTML */
//$x = new html('Google');

/* Creo link sobre el HTML on_the_fly */
$h = new link('Google');
$h->setUri('http://google.es');
$h->linkear();

/* Le hago un strong */
$h = new strong ($h);
$h->render();

/* Le hago italica   */
$h = new italic ($h);
$h->render();

echo 
$h;

/* Creo imagen y le aplico enlace */
$i = new img ('http://2.bp.blogspot.com/-nURw-T8ErDM/ThYBg4eyhGI/AAAAAAAAB8I/Tapme43aapE/s72-c/google-picasa-blogger.jpg');
$i = new link($i);
$i->setUri('http://google.es');
$i->linkear();

echo 
$i;
Salida de la primera parte:
Cita:
<i><strong><a href='http://www.google.com'>Google</a></strong></i>
GRACIAS
__________________
Salu2!

Última edición por Italico76; 17/07/2011 a las 19:37