Home  >  Article  >  Backend Development  >  php decoration mode

php decoration mode

WBOY
WBOYOriginal
2016-08-08 09:23:571101browse

Decoration Mode of Transformers

(1) Abstract construction class Tansform

interface Transform
{
	public function move();
}

(2) Concrete construction class Car

final class Car implements Transform
{
	public function __construct() {
		echo '变形金刚是一辆汽车';
	}
	public function move() {
		echo '在陆地上移动';
	}
}
(3) Abstract decoration class Changer

class Changer implements Transform
{
	private $transform;
	public function __construct($tansform='') {
		$this->transform = $tansform;
	}
	public function move() {
		$this->transform->move();
	}
}
(4) Specific decoration type Root, Airplane

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 '在天空飞翔';
	}
}

(5) Test Code

$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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn