PHP之装饰器模式
2008-09-08 16:00
204 查看
<?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());
?>
相关文章推荐
- PHP、Python和Javascript的装饰器模式对比
- PHP设计模式之装饰器模式
- php装饰模式
- 慕课网----大话PHP设计模式 五(原型模式,装饰器模式,迭代器模式,代理模式)
- PHP设计模式中之装饰模式
- PHP设计模式之装饰器模式
- php设计模式-装饰器模式
- php设计模式之装饰模式
- PHP、Python和Javascript的装饰器模式对比
- 【深入PHP 面向对象】读书笔记(九) - 让面向对象编程更加灵活的模式(二) - 装饰模式
- [php]php设计模式 Decorator(装饰模式)
- PHP试写装饰模式(Java、C#亦同)
- php 装饰模式
- php 设计模式 之 装饰模式
- php八大设计模式之装饰器模式
- PHP设计模式 装饰器模式
- PHP 装饰器模式
- PHP设计模式之装饰器模式定义与用法详解
- PHP设计模式之装饰模式