装饰器设计模式(来源于php设计模式 看书笔记)
何时使用装饰器
关于包装器
下面代码显示了如何将一个整数包装在一个对象中,以及如何获取这个整数 包装器表示用来减少不兼容性的策略
class primitiveWrap {
private $wrapMe ;
public function __construct ($wrapMe ) {
$this ->wrapMe;
}
public function showWrap () {
return $this -wrapMe;
}
}
$myPrim = 521 ;
$wrappedUp = new PrimitiveWrap($myPrim );
echo $wrappedUp ->showWrap();
php 中内置的包装器
file_get_contents 将一个文件包装在内置的包装器中。
设计模式包装器
下面的代码展示了Client 如何将组件对象($component) 包装在装饰器(Maintenance)中; $component = new Maintenance($component);
包装多个组件的装饰器
开发人员的约会服务
它为软件开发人员建立一个约会服务,两个组件分别是Male和Female,可以分别为这两个组件装饰不同的约会地点 每个组件都有一个名字和指定的年龄,具体装饰会有不同的状态 组件接口
包括3个属性和5个方法 $date 属性用来标识这是一个“约会”,而不是普通的日期对象 $ageGroup 是该组件所属的组 $feature 是由某个具体装饰提供的特性
abstract class IComponent {
protected $date ;
protected $ageGroup ;
protected $feature ;
abstract public function setAge ($ageNow ) ;
abstract public function getAge () ;
abstract public function setFeature () ;
abstract public function getFeature () ;
}
class Male extends IComponent {
function __construct () {
$this ->date = "Male" ;
$this ->setFeature("Dude programmer features:" );
}
public function setAge ($ageNow ) {
$this ->ageGroup = $ageNow ;
} public function getAge () {
return $this ->ageGroup;
}
public function setFeature ($fea ) {
$this ->feature = $fea ;
}
public function getFeature () {
return $this ->feature;
}
}
class Female extends IComponent {
function __construct () {
$this ->date = "Female" ;
$this ->setFeature("Grrrl programmer features:" );
}
public function setAge ($ageNow ) {
$this ->ageGroup = $ageNow ;
}
public function getAge () {
return $this ->ageGroup;
}
public function setFeature ($fea ) {
$this ->feature = $fea ;
}
public function getFeature () {
return $this ->feature;
}
}
abstract class Decorator extends IComponent {
public function setAge ($ageNoe ) {
$this ->ageGroup=$this ->ageGroup;
}
public function getAge () {
return $this ->ageGroup;
}
}
class ProgramLang extends Decorator {
private $languageNow ;
private $language = array ( "php" => "PHP" , "cs" => "C#" , "js" => "JavaScript" , );
public function __construct (IComponent $dateNow ) {
$this ->date = $dateNow ;
}
public function setFeature ($lan ) {
$this ->languageNow = $this ->language[$lan ];
}
public function getFeature () {
$output = $this ->date->getFeature();
$fmat = "<br /> " ;
$output .= "$fmat preferred programming language:" ;
$output .= $this ->languageNow;
return $output ;
}
}
function __autoload ($class_name ) {
include $class_name .'.php' ;
}
class Client {
private $hotDate ;
public function __contruct () {
$this ->hotDate = new Female();
$this ->hotDate->setAge("Age group 4" );
echo $this ->hotDate->getAge();
$this ->hotDate->getFeature();
echo $this ->hotDate->getFeature();
}
private function wrapComponent (IComponent $component ) {
$component = new ProgramLang($component );
$component ->setFeature("php" );
$component = new Hardware($component );
$component ->setFeature("lin" );
$component = new Food($component );
$component ->setFeature("veg" );
retrun $component ;
}
}
$worker = new Client();