<?php
abstract class AbstractRating {
protected $decoratable;
public function __construct( $decoratable ) {
$this -> decoratable = $decoratable;
}
abstract public function getRating();
}
class PoorRating extends AbstractRating {
protected $rating = 1;
public function __construct($decoratable) {
parent::__construct( $decoratable );
}
public function getRating() {
return $this -> decoratable -> getRating() + $this -> rating;
}
}
class AverageRating extends AbstractRating {
protected $rating = 2;
public function __construct( $decoratable ) {
parent::__construct( $decoratable );
}
public function getRating() {
return $this -> decoratable -> getRating() + $this -> rating;
}
}
class GoodRating extends AbstractRating {
protected $rating = 3;
public function __construct( $decoratable ) {
parent::__construct( $decoratable );
}
public function getRating() {
return $this -> decoratable -> getRating() + $this -> rating;
}
}
class Ratings {
private $rating = 3;
public function __construct() { }
public function getRating() {
return $this -> rating; // typeof integer
}
}
$rating = new GoodRating( new AverageRating( new Ratings ) ); // we are decorating the Ratings object
echo($rating -> getRating());
?>
abstract class AbstractRating {
protected $decoratable;
public function __construct( $decoratable ) {
$this -> decoratable = $decoratable;
}
abstract public function getRating();
}
class PoorRating extends AbstractRating {
protected $rating = 1;
public function __construct($decoratable) {
parent::__construct( $decoratable );
}
public function getRating() {
return $this -> decoratable -> getRating() + $this -> rating;
}
}
class AverageRating extends AbstractRating {
protected $rating = 2;
public function __construct( $decoratable ) {
parent::__construct( $decoratable );
}
public function getRating() {
return $this -> decoratable -> getRating() + $this -> rating;
}
}
class GoodRating extends AbstractRating {
protected $rating = 3;
public function __construct( $decoratable ) {
parent::__construct( $decoratable );
}
public function getRating() {
return $this -> decoratable -> getRating() + $this -> rating;
}
}
class Ratings {
private $rating = 3;
public function __construct() { }
public function getRating() {
return $this -> rating; // typeof integer
}
}
$rating = new GoodRating( new AverageRating( new Ratings ) ); // we are decorating the Ratings object
echo($rating -> getRating());
?>