Decoration Mode of Transformers
(1) Abstract construction class Tansform
interface Transform { public function move(); }
final class Car implements Transform { public function __construct() { echo '变形金刚是一辆汽车'; } public function move() { echo '在陆地上移动'; } }
class Changer implements Transform { private $transform; public function __construct($tansform='') { $this->transform = $tansform; } public function move() { $this->transform->move(); } }
class Root extends Changer { public function __construct($tansform='') { parent::__construct($tansform); echo '变成机器人'; } public function say() { echo '说话'; } } class Airplane extends Changer { public function __construct($tansform='') { parent::__construct($tansform); echo '变成机飞机'; } public function fly() { echo '在天空飞翔'; } }
$camaro = new Car(); echo '<br>'; $camaro->move(); echo '<br>'; echo '-----------'; echo '<br>'; $bumblebee = new Airplane($camaro); echo '<br>'; $bumblebee->move(); echo '<br>'; $bumblebee->fly(); echo '<br>'; echo '-----------'; echo '<br>'; $bumblebee = new Root($camaro); echo '<br>'; $bumblebee->move(); echo '<br>'; $bumblebee->say();
Transformers is a car
that moves on land
----------
turns into an airplane
Move on land
Fly in the sky
----------
Become a robot
Move on land
Speak
The above introduces the PHP decoration mode, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.