php组合模式

组合模式应用于递归结构的优化或分层数据结构的管理:

创新互联是一家集网站建设,西秀企业网站建设,西秀品牌网站建设,网站定制,西秀网站建设报价,网络营销,网络优化,西秀网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

abstract class Compary

{

protected $name;

/* public function __construct($name)

{

$this->name=$name;

}  */

abstract public  function Add(Compary $compary);

abstract public  function Move(Compary $compary);

abstract public  function Display($level=0);

}

class SubCompary extends Compary

{

private $subcompary=array();

//protected $name;

public function __construct($name)

{

//parent::$name=$name;

$this->name=$name;

}

public function Add(Compary $compary)

{

if(!$compary)

{

return false;

}

$key=array_search($compary,$this->subcompary);

if($key===false)

{

$this->subcompary[]=$compary;

}

return true;

}

public function Move(Compary $compary)

{

if(!$compary)

{

return false;

}

$key=array_search($compary,$this->subcompary);

if($key!==false)

{

unset($this->subcompary[$key]);

}

return true;

}

public function Display($level=0)

{

//输出本节点的名称

if($this->name)

{

$pre='';

for($i=0;$i<$level;$i++)

{

$pre.='-';

}

echo $pre.$this->name.'
';

}

//按层次输出每个分层的节点的名称

if(is_array($this->subcompary))

{

foreach($this->subcompary as $key => $value )

{

//递归输出每层的节点名称

//递归输出每层的节点名称

$value->Display($level+2);

}

}

else

{

}

}

}

class Department extends Compary

{

private $department=array();

//protected $name;

public function __construct($name)

{

//parent::$name=$name;

$this->name=$name;

}

public function Add(Compary $compary)

{

/* if(!$compary)

{

return false;

}

$key=array_search($compary,$this->dapartment);

if($key===false)

{

$this->dapartment[]=$compaty;

return true;

}

else

{

return false;

} */

echo '叶子节点不能增加节点!';

}

public function Move(Compary $compary)

{

/* if(!$compary)

{

return false;

}

$key=array_search($compary,$this->department);

if($key!==false)

{

unset($this->department[$key]);

return true;

}

else

{

return false;

} */

echo '叶子节点不能删除节点!';

}

public function Display($level=0)

{

if(!$this->name)

{

return false;

}

$pre='';

for($i=0;$i<$level;$i++)

{

$pre.='-';

}

echo $pre.$this->name.'
';

/* if(is_array($this->department))

{

foreach($this->department as $key => $value )

{

$value->Display($level+2);

}

}

else

{

return false;

} */

}

}

header('content-type:text/html;charset=utf-8');

$subcompary=new SubCompary('北京公司');

$subcompary->Add(new Department('总公司财务部'));

$subcompary->Add(new Department('总公司技术部'));

$shanghai=new SubCompary('上海分公司');

$shanghai->Add(new Department('上海分公司技术部'));

$shanghai->Add(new Department('上海分公司财务部'));

$subcompary->Add($shanghai);

$subcompary->Display(2);

?>


文章题目:php组合模式
文章网址:http://azwzsj.com/article/ghdhpg.html