文章摘要
文章介绍了组件化设计的实现方法,通过定义抽象组件类 `AbstractClass` 来实现组件接口。`AbstractClass` 包含 `name` 属性和三个抽象方法 `add()`、`remove()` 和 `display()`,用于统一定义组件的基本功能。 `Leaf` 类继承自 `AbstractClass`,实现了无法添加或移除子组件的功能,并通过 `display()` 方法输出层级结构。 `Composite` 类继承自 `AbstractClass`,实现了一个容器组件的功能,可以存储多个子组件,并通过递归调用 `display()` 方法展示层级结构。文章还通过客户端代码展示了如何使用这些组件实现层级展示功能。
<?php
// 抽象组件
abstract class Component
{
protected $name;
public function __construct($name)
{
$this->name=$name;
}
abstract public function add(Component $component);
abstract public function remove(Component $component);
abstract public function display($depth);
}
// 叶子组件
class Leaf extends Component
{
public function add(Component $component)
{
echo “Cannot add to a leaf.”;
}
public function remove(Component $component)
{
echo “Cannot remove from a leaf.”;
}
public function display($depth)
{
echo str_repeat(“-“, $depth) . $this->name . “\n”;
}
}
// 容器组件
class Composite extends Component
{
private $children=array();
public function add(Component $component)
{
array_push($this->children, $component);
}
public function remove(Component $component)
{
$key=array_search($component, $this->children, true);
if ($key !==false) {
unset($this->children[$key]);
}
}
public function display($depth)
{
echo str_repeat(“-“, $depth) . $this->name . “\n”;
foreach ($this->children as $component) {
$component->display($depth + 2);
}
}
}
// 客户端代码
$root=new Composite(“root”);
$root->add(new Leaf(“Leaf A”));
$root->add(new Leaf(“Leaf B”));
$comp=new Composite(“Composite X”);
$comp->add(new Leaf(“Leaf XA”));
$comp->add(new Leaf(“Leaf XB”));
$root->add($comp);
$root->add(new Leaf(“Leaf C”));
$leaf=new Leaf(“Leaf D”);
$root->add($leaf);
$root->remove($leaf);
$root->display(1);
// 抽象组件
abstract class Component
{
protected $name;
public function __construct($name)
{
$this->name=$name;
}
abstract public function add(Component $component);
abstract public function remove(Component $component);
abstract public function display($depth);
}
// 叶子组件
class Leaf extends Component
{
public function add(Component $component)
{
echo “Cannot add to a leaf.”;
}
public function remove(Component $component)
{
echo “Cannot remove from a leaf.”;
}
public function display($depth)
{
echo str_repeat(“-“, $depth) . $this->name . “\n”;
}
}
// 容器组件
class Composite extends Component
{
private $children=array();
public function add(Component $component)
{
array_push($this->children, $component);
}
public function remove(Component $component)
{
$key=array_search($component, $this->children, true);
if ($key !==false) {
unset($this->children[$key]);
}
}
public function display($depth)
{
echo str_repeat(“-“, $depth) . $this->name . “\n”;
foreach ($this->children as $component) {
$component->display($depth + 2);
}
}
}
// 客户端代码
$root=new Composite(“root”);
$root->add(new Leaf(“Leaf A”));
$root->add(new Leaf(“Leaf B”));
$comp=new Composite(“Composite X”);
$comp->add(new Leaf(“Leaf XA”));
$comp->add(new Leaf(“Leaf XB”));
$root->add($comp);
$root->add(new Leaf(“Leaf C”));
$leaf=new Leaf(“Leaf D”);
$root->add($leaf);
$root->remove($leaf);
$root->display(1);
© 版权声明
文章版权归作者所有,未经允许请勿转载。



