交接模式之模拟毛笔
(1) 实现类接口
interface Color { public function bepaint($penType,$name); }
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; } }
abstract class Pen { protected $color; public function setColor($Color) { $this->color = $Color; } abstract function draw($name); }
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); } }
$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/>"; }
<?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教程有兴趣的朋友有所帮助。