首頁  >  文章  >  後端開發  >  php 橋接模式

php 橋接模式

WBOY
WBOY原創
2016-08-08 09:24:191128瀏覽

交接模式之模拟毛笔

(1) 实现类接口

interface Color
{
	public function bepaint($penType,$name);
}

(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;
	}
}

(3)抽象类Pen

abstract class 	Pen {
	protected $color;
	public function setColor($Color) {
		$this->color = $Color;
	}
	abstract function draw($name);
}
(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);
	}
}

(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/>";
}

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> 

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

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn