回到主题,现在有一个需求,客户需要创建一个叶子,可以设定叶子大小和颜色,而且可以为叶子起名。
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] => 黄
)
)
)
)
)
)
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());