您好,欢迎访问一九零五行业门户网

php设计模式:桥接模式学习心得(附案例代码)

假如你有一个形状类(shape),目前它扩展出了两个子类,圆形类和正方形类。伪代码如下:
interface shape{ function fill ();}class circleshage implements shape{ public function fill () { echo '圆形' . php_eol; }}class squareshape implements shape{ public function fill () { echo '正方形' . php_eol; }}
现在你想给形状增加颜色——红色、蓝色。那么,你应该如何做,如果用继承的思想,那么就需要写出四个类,分别如下:
class redcircleshage implements shape{ public function fill () { echo '红色圆形' . php_eol; }}class redsquareshape implements shape{ public function fill () { echo '红色正方形' . php_eol; }}class bluecircleshage implements shape{ public function fill () { echo '蓝色圆形' . php_eol; }}class bluesquareshape implements shape{ public function fill () { echo '蓝色正方形' . php_eol; }}
但现在如果我想加入新的形状——三角形,新的颜色——黑色以及白色。那么我们就需要12个子类(类爆炸),那么有没有什么好的办法来解决呢?
合成(组合)与聚合
解决上述问题前,我们想理解合成与聚合的含义。
合成聚合原则:尽量使用合成/聚合,尽量不要使用继承。
合成(组合):表示一种整体与部分的关系(强关系),它们具有相同的生命周期,比如人和心脏的关系,心脏是人的一部分。
聚合:表示一种整体与部分的关系(弱关系),表示a对象可以包含b对象,但b对象并不是a对象的一部分。比如,我们上面所说的形状和颜色,形状可以包含颜色,但颜色并不是形状的一部分。
桥接模式,就是使用聚合来对系统进行解耦的。
桥接模式
定义:将抽象部分与它的实现分离,使他们可以独立的变化
上面的定义很难理解,用我们上面的例子就是,一个系统可能有多个角度分类(颜色、形状),每一种分类都可能有变化(新增新的形状和颜色),那么我们就可以用多个角度将系统的实现分离出来,降低他们之间的耦合。
那么如何来实现呢?下面是实现代码:
abstract class fshape{ protected $color = null; public function __construct(icolor $color) { $this->color = $color; } public function shape () { echo $this->color->color() . $this->setshape() . php_eol; } protected abstract function setshape ();}class circleshape extends fshape{ protected function setshape () { return '圆形'; }}class squareshape extends fshape{ protected function setshape () { return '正方形'; }}interface icolor{ public function color ();}class bluecolor implements icolor{ public function color () { return '蓝色'; }}class redcolor implements icolor{ public function color () { return '红色'; }}
测试代码如下:
$bluecolor = new bluecolor();$redcolor = new redcolor();$bluecircleshape = new circleshape($bluecolor);$redcircleshape = new circleshape($redcolor);$bluesquareshape = new squareshape($bluecolor);$redsquareshape = new squareshape($redcolor);$bluecircleshape->shape();$redcircleshape->shape();$bluesquareshape->shape();$redsquareshape->shape();
使用桥接模式后,我们的代码就符合了“开闭原则”, 当有新的形状或颜色加入时,我们只需要添加新的类即可。而不用去修改之前的类。
学习推荐:
php视频教程
mysql视频教程
以上就是php设计模式:桥接模式学习心得(附案例代码)的详细内容。
其它类似信息

推荐信息