php bridge mode

WBOY
Release: 2016-08-08 09:24:19
Original
1137 people have browsed it

交接模式之模拟毛笔

(1) 实现类接口

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

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

(3)抽象类Pen

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

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

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> 
Copy after login

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

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

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