PHP设计模式组合与迭代器
组合模式和迭代器模式都是结构型设计模式。组合模式让客户端统一处理单个对象和组合对象,迭代器提供遍历集合的标准方式。今天说说这两种模式。
组合模式在树形结构中很有用。
```php
abstract class FileSystemComponent
{
protected string $name;
public function __construct(string $name) { $this->name = $name; }
abstract public function getSize(): int;
abstract public function display(int $depth = 0): void;
}
class File extends FileSystemComponent
{
private int $size;
public function __construct(string $name, int $size)
{
parent::__construct($name);
$this->size = $size;
}
public function getSize(): int { return $this->size; }
public function display(int $depth = 0): void
{
echo str_repeat(" ", $depth) . "📄 {$this->name} ({$this->size}KB)\n";
}
}
class Directory extends FileSystemComponent
{
private array $children = [];
public function add(FileSystemComponent $component): void
{
$this->children[] = $component;
}
public function getSize(): int
{
$total = 0;
foreach ($this->children as $child) $total += $child->getSize();
return $total;
}
public function display(int $depth = 0): void
{
echo str_repeat(" ", $depth) . "📁 {$this->name}/\n";
foreach ($this->children as $child) $child->display($depth + 1);
}
}
$root = new Directory("root");
$home = new Directory("home");
$home->add(new File("readme.txt", 10));
$home->add(new File("photo.jpg", 500));
$root->add($home);
$root->add(new File("config.php", 50));
$root->display();
echo "总大小: {$root->getSize()}KB\n";
?>
迭代器提供统一的遍历方式。
```php
class RangeIterator implements Iterator
{
private int $current;
private int $end;
private int $step;
public function __construct(int $start, int $end, int $step = 1)
{
$this->current = $start;
$this->end = $end;
$this->step = $step;
}
public function current(): mixed { return $this->current; }
public function key(): mixed { return $this->current; }
public function next(): void
{
$this->current += $this->step;
}
public function rewind(): void
{
$this->current = 0;
}
public function valid(): bool
{
return $this->current <= $this->end;
}
}
foreach (new RangeIterator(1, 5) as $num) {
echo "$num ";
}
echo "\n";
?>
组合模式让叶子节点和容器节点有统一的接口。迭代器让集合的遍历方式统一。两种模式在框架和库中广泛应用,PHP的SPL提供了多种迭代器实现。
PHP设计模式组合与迭代器
张小明
前端开发工程师
AI 视频抽卡时代落幕,可编辑时代开启,创作者角色将从剪辑转向“模型导演”
抽卡时代要过去了过去一年多,对 AI 视频的体感可以用“抽卡”概括。输入 prompt 生成画面,好看就留下,不好看就重改。它虽能产出惊艳片段,但给创作者的不是能接着干活的素材,而是一张抽到就走、抽不到就重抽的卡。抽卡…
保姆级教程:用AWS CLI在Windows上快速下载CSE-CIC-IDS2018数据集(附完整命令清单)
网络安全研究者的实战指南:高效获取CSE-CIC-IDS2018数据集第一次接触网络安全数据分析时,最令人头疼的往往不是算法或工具,而是如何快速获取高质量的数据集。CSE-CIC-IDS2018作为网络安全领域的标杆数据集,包含了丰富的网络攻击流…
告别S3控制台!用MinIO Client(mc)命令行高效管理你的对象存储(附常用命令速查表)
命令行革命:用MinIO Client彻底重构你的对象存储工作流当你在凌晨三点被服务器告警惊醒,需要紧急修复一个存储桶权限问题时,最后一次点击网页控制台的耐心早已耗尽。这就是为什么全球顶尖的运维团队正在将MinIO Client(mc…
ATK GEAR游戏键盘选购导航:从入门到旗舰的精准指南
摘要 选择一款合适的磁轴键盘,关键在于明确你的游戏场景、预算和功能偏好。对于追求极致响应速度的FPS玩家,ATK GEAR的第三代烈风ULTRA自研方案提供了0.28ms超低延迟和0.001mm行程精度;对于需要兼顾学习和娱乐的大学生,其68键/75%…
Kubernetes 网络策略深度解析:从原理到生产落地实践
Kubernetes 网络策略深度解析:从原理到生产落地实践一、微服务集群的网络隔离困境:从一次故障谈起 在大规模微服务架构中,网络安全与访问控制是运维工作的重中之重。没有哪台服务器是一次重启解决不了的,如果有,那就是…
VB程序老崩溃?这套错误处理机制让我三年没出过事故
VB程序老崩溃?这套错误处理机制让我三年没出过事故 写VB的人分两种:一种是程序报错了才知道哪里出问题,另一种是程序还没崩就已经把异常兜住了。我以前属于第一种,做的库存管理系统上线第一天就崩了三次,客户打电话过来…