每日一模式之裝飾器模式

<?php
//裝飾器模式:不改變數據結構,只作一些細微的修改
class Words{
	public $my_txt;
	public function __construct($txt){
		$this->my_txt = $txt;
	}
}

class DecoratorWords{

	public function Decorator(Words $word){
		$word->my_txt = strtoupper($word->my_txt);
		return $word;
	}
}

$docorator_obj = new DecoratorWords();
echo $docorator_obj->Decorator(new Words("hello world!"))->my_txt;
相關文章
相關標籤/搜索