这个模式理解起来会有些歧义,特别是某些书上面那些难懂的阐述。先来说说组合模式的几个特点:
1、必须存在不可分割基本元素。
2、组合后的物体可以被组合。
举个通俗的例子,原子是化学反应的基本微粒,它在化学反应中不可分割。现在有 c(碳)、h(氢)、o(氧)、n(氮)4种原子,它们可以随机组合成无数种分子,可以是蛋白质,也可以是脂肪,蛋白质和脂肪就是组合。由蛋白质和脂肪又可以一起被组合成肉、大豆等等。
回到主题,现在有一个需求,客户需要创建一个叶子,可以设定叶子大小和颜色,而且可以为叶子起名。
abstract class tree{
abstract function create();
}
class createleaf extends tree{
private $name;
private $size;
private $color;
private $leaf=array();
public function __construct($name,$size,$color){
$this->name=$name;
$this->size=$size;
$this->color=$color;
}
public function create(){
$this->leaf[$this->name]=array(
'size'=>$this->size,
'color'=>$this->color
);
return $this->leaf;
}
}
$leaf=new createleaf('大红树叶','大','红');
print_r($leaf->create());
运行以上代码将得到:
array
(
[大红树叶] => array
(
[size] => 大
[color] => 红
)
)
我们的设计完美的实现了客户的需求,但是,现在客户的新要求来了,不仅要可以创建叶子,还要可以创建树枝,而且,可以把叶子安插在树枝上,也可以把安插好的叶子从树枝上拆下来。他们最终想要结果是,树枝上可以安插其他的树枝,从而构建出一颗枝繁叶茂的大树
分析:创建叶子和创建树枝都拥有创建操作,所以它们都可以对抽象tree类进行实现,但创建树枝的类还需要安插和拆除的操作,所以我们暂且在tree类中加上两个抽象方法combination() 和 separation()。
abstract class tree{
abstract function create();//创建
abstract function combination(tree $item);//组合
abstract function separation(tree $item);//分离
}
class createleaf extends tree{
private $name;
private $size;
private $color;
private $leaf=array();
public function __construct($name,$size,$color){
$this->name=$name;
$this->size=$size;
$this->color=$color;
}
public function create(){
$this->leaf[$this->name]=array(
'size'=>$this->size,
'color'=>$this->color
);
return $this->leaf;
}
//由于创建叶子类不需要组合和分离的操作,我们将这两个方法投掷出错误警告。
public function combination(tree $item){
throw new exception("本类不支持组合操作");
}
public function separation(tree $item){
throw new exception("本类不支持分离操作");
}
}
class createbranch extends tree{
private $name;
private $branch=array();
private $items=array();//树枝可能被安插叶子,该变量用于存放叶子对象
public function __construct($name){
$this->name=$name;
}
//我们已经知道$items内的对象都包含创建操作,所以只要依次执行各对象的创建操作,收集结果便可
public function create(){
foreach($this->items as $item){
$arr=$item->create();
$this->branch[$this->name][]=$arr;
}
if(empty($this->branch)){
$this->branch[$this->name]=array();
}
return $this->branch;
}
public function combination(tree $item){
$this->items[]=$item;
}
public function separation(tree $item){
$key=array_search($item,$this->items);
if($key!==false){
unset($this->items[$key]);
}
}
}
$leaf_1=new createleaf('大红树叶','大','红');
$leaf_2=new createleaf('大绿树叶','大','绿');
$leaf_3=new createleaf('大黄树叶','大','黄');
$leaf_4=new createleaf('小红树叶','小','红');
$leaf_5=new createleaf('小绿树叶','小','绿');
$leaf_6=new createleaf('小黄树叶','小','黄');
$branch_1=new createbranch('树枝1号');
$branch_1->combination($leaf_1);
$branch_1->combination($leaf_2);
$branch_1->combination($leaf_3);
$branch_2=new createbranch('树枝2号');
$branch_2->combination($leaf_4);
$branch_2->combination($leaf_5);
$branch_2->combination($leaf_6);
$branch=new createbranch('树干');
$branch->combination($branch_1);
$branch->combination($branch_2);
print_r($branch->create());
运行以上代码将得到:
array
(
[树干] => array
(
[0] => array
(
[树枝1号] => array
(
[0] => array
(
[大红树叶] => array
(
[size] => 大
[color] => 红
)
)
[1] => array
(
[大绿树叶] => array
(
[size] => 大
[color] => 绿
)
)
[2] => array
(
[大黄树叶] => array
(
[size] => 大
[color] => 黄
)
)
)
)
[1] => array
(
[树枝2号] => array
(
[0] => array
(
[小红树叶] => array
(
[size] => 小
[color] => 红
)
)
[1] => array
(
[小绿树叶] => array
(
[size] => 小
[color] => 绿
)
)
[2] => array
(
[小黄树叶] => array
(
[size] => 小
[color] => 黄
)
)
)
)
)
)
我们漂亮的完成了这个需求,一颗苍天大树被我们创建
,但这里有一个问题,创建树叶操作只需要create() 操作,并不需要combination() 和 separation(),我们为何不把抽象类tree拆分为两个类呢?
abstract class tree{
abstract function create();
}
//拆分出的树干抽象类,由于继承自tree,必须将create()实现,但实现create()又会造成代码重复,所以将此类也申明为抽象类
abstract class branch extends tree{
abstract function combination(tree $item);
abstract function separation(tree $item);
}
class createleaf extends tree{
private $name;
private $size;
private $color;
private $leaf=array();
public function __construct($name,$size,$color){
$this->name=$name;
$this->size=$size;
$this->color=$color;
}
public function create(){
$this->leaf[$this->name]=array(
'size'=>$this->size,
'color'=>$this->color
);
return $this->leaf;
}
public function combination(tree $item){
throw new exception("本类不支持组合操作");
}
public function separation(tree $item){
throw new exception("本类不支持分离操作");
}
}
class createbranch extends branch{
private $name;
private $branch=array();
private $items=array();
public function __construct($name){
$this->name=$name;
}
public function create(){
foreach($this->items as $item){
$arr=$item->create();
$this->branch[$this->name][]=$arr;
}
if(empty($this->branch)){
$this->branch[$this->name]=array();
}
return $this->branch;
}
public function combination(tree $item){
$this->items[]=$item;
}
public function separation(tree $item){
$key=array_search($item,$this->items);
if($key!==false){
unset($this->items[$key]);
}
}
}
$leaf_1=new createleaf('大红树叶','大','红');
$leaf_2=new createleaf('大绿树叶','大','绿');
$leaf_3=new createleaf('大黄树叶','大','黄');
$leaf_4=new createleaf('小红树叶','小','红');
$leaf_5=new createleaf('小绿树叶','小','绿');
$leaf_6=new createleaf('小黄树叶','小','黄');
$branch_1=new createbranch('树枝1号');
$branch_1->combination($leaf_1);
$branch_1->combination($leaf_2);
$branch_1->combination($leaf_3);
$branch_2=new createbranch('树枝2号');
$branch_2->combination($leaf_4);
$branch_2->combination($leaf_5);
$branch_2->combination($leaf_6);
$branch=new createbranch('树干');
$branch->combination($branch_1);
$branch->combination($branch_2);
print_r($branch->create());
这样,我们总算是漂亮的完成了这个需求。但必须注意的是,由于组合模式的灵活性,很多人喜欢不假思索的使用组合类。事实上,组合类存在着“过于灵活”、“开销大”的缺陷。我们试想一下,一个元素或组合在整个系统中可能被调用非常多次,但一旦某个元素或组合在系统中的一个节点出现问题,我们将很难排查到那个节点。
再试想一下,若是系统中的某个元素是一条查询数据库的sql语句,而且这条sql语句的开销有些大,一旦它被组合到整个系统的每一个角落,运行系统造成的结果将是灾难性的。
以上就是php面向对象开发之——组合模式的内容。