php 桥接模式

WBOY
Lepaskan: 2016-08-08 09:24:19
asal
1138 orang telah melayarinya

交接模式之模拟毛笔

(1) 实现类接口

interface Color
{
	public function bepaint($penType,$name);
}
Salin selepas log masuk

(2) 具体实现颜色类

class Red implements Color
{
	public function bepaint($penType, $name) {
		echo $penType.'红色的'.$name;
	}
}
class Green implements Color
{
	public function bepaint($penType, $name) {
		echo $penType.'绿色的'.$name;
	}
}
class Blue implements Color
{
	public function bepaint($penType, $name) {
		echo $penType.'蓝色的'.$name;
	}
}
class White implements Color
{
	public function bepaint($penType, $name) {
		echo $penType.'白色的'.$name;
	}
}
class Black implements Color
{
	public function bepaint($penType, $name) {
		echo $penType.'黑色的'.$name;
	}
}
Salin selepas log masuk

(3)抽象类Pen

abstract class 	Pen {
	protected $color;
	public function setColor($Color) {
		$this->color = $Color;
	}
	abstract function draw($name);
}
Salin selepas log masuk
(4) 扩充抽象类

class BigPen extends Pen
{
	public function draw($name) {
		$penType = '大号铅笔绘制';
		$this->color->bepaint($penType,$name);
	}
}
class MiddlePen extends Pen
{
	public function draw($name) {
		$penType = '中号铅笔绘制';
		$this->color->bepaint($penType,$name);
	}
}
class SmallPen extends Pen
{
	public function draw($name) {
		$penType = '小号铅笔绘制';
		$this->color->bepaint($penType,$name);
	}
}
Salin selepas log masuk

(5) 辅助代码

$string = simplexml_load_file('config.xml');
foreach ($string as $v) {
	eval("\$pen = new {$v->type}();");
	eval("\$color = new {$v->color}();");
	$pen->setColor($color);
	$pen->draw("鲜花");
	echo "<br>";
}
Salin selepas log masuk

config.xml

<?xml version="1.0" encoding="UTF-8"?>  
  <config>  
	  <one>  
		  <color>Red</color>  
		  <type>BigPen</type>
	  </one> 
	  <two>  
		  <color>Blue</color>  
		  <type>SmallPen</type>
	  </two>  	  
  </config> 
Salin selepas log masuk

大号铅笔绘制红色的鲜花
小号铅笔绘制蓝色的鲜花

以上就介绍了php 桥接模式,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan