欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 桥接模式

程序员文章站 2022-04-12 10:58:18
...
交接模式之模拟毛笔

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

config.xml

RedBigPenBlueSmallPen

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

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