Home > Backend Development > PHP Tutorial > php decoration mode

php decoration mode

WBOY
Release: 2016-08-08 09:23:57
Original
1128 people have browsed it

Decoration Mode of Transformers

(1) Abstract construction class Tansform

interface Transform
{
	public function move();
}
Copy after login

(2) Concrete construction class Car

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

class Changer implements Transform
{
	private $transform;
	public function __construct($tansform='') {
		$this->transform = $tansform;
	}
	public function move() {
		$this->transform->move();
	}
}
Copy after login
(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 '在天空飞翔';
	}
}
Copy after login

(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();
Copy after login


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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template