从顶级开源项目源码中提炼的代码级编程模式。
交互式可视化(支持时间旅行回放)· 精确到行号的源码链接 · 多语言实现 · 可运行练习
English | 简体中文

| **🧠 数据结构** - [Bitmask](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/bitmask/) — 标志位压缩 - [Min Heap](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/min-heap/) — 优先队列 - [Ring Buffer](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/ring-buffer/) — 定长 FIFO - [Trie](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/trie/) — 前缀搜索 - [Skip List](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/skip-list/) — 概率有序 - [Bloom Filter](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/bloom-filter/) — 集合判存 - [LRU Cache](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/lru-cache/) — 淘汰策略 - [B+ Tree](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/b-plus-tree/) — 磁盘优化索引 - [Tagged Union](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/tagged-union/) — 类型安全分发 - [Merkle Tree](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/merkle-tree/) — 完整性证明 - [Merge Iterator](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/merge-iterator/) — K 路合并 | **⚡ 并发** - [Semaphore](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/semaphore/) — 有界访问 - [Actor Model](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/actor-model/) — 消息传递 - [Work Stealing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/work-stealing/) — 负载均衡 - [MVCC](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/mvcc/) — 快照隔离 - [Cooperative Scheduling](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/cooperative-scheduling/) — 协作让出 - [Double Buffering](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/double-buffering/) — 原子交换 - [Backpressure](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/backpressure/) — 流量控制 - [Event Loop](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/event-loop/) — I/O 多路复用 - [Logical Clock](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/logical-clock/) — 事件排序 | **🏗️ 系统** - [Circuit Breaker](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/circuit-breaker/) — 容错 - [Rate Limiter](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/rate-limiter/) — 限流 - [Retry Backoff](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/retry-backoff/) — 弹性 - [Write-Ahead Log](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/write-ahead-log/) — 持久性 - [Batch Processing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/batch-processing/) — 吞吐 - [Consistent Hashing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/consistent-hashing/) — 分布 - [Dependency Graph](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/dependency-graph/) — 排序 - [Middleware Chain](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/middleware-chain/) — 管道 - [Registry](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/registry/) — 自注册 - [Dirty Flag](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/dirty-flag/) — 延迟重算 - [LSM Tree](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/lsm-tree/) — 写优化存储 - [Checkpointing](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/checkpointing/) — 快照恢复 |
| **♻️ 内存** - [Object Pool](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/object-pool/) — 复用实例 - [Flyweight](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/flyweight/) — 共享不可变 - [Arena Allocator](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/arena-allocator/) — bump 分配 - [Free List](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/free-list/) — O(1) 分配释放 - [Copy-on-Write](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/copy-on-write/) — 延迟复制 - [Reference Counting](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/reference-counting/) — 自动清理 - [Tombstone](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/tombstone/) — 延迟删除 - [Interning](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/interning/) — 去重驻留 | **🔄 行为型** - [State Machine](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/state-machine/) — 状态转换 - [Observer](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/observer/) — 发布订阅 - [Iterator](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/iterator/) — 惰性求值 - [Diff / Patch](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/diff-patch/) — 最小编辑 - [Vtable](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/vtable/) — 手动多态 - [Visitor](https://totoro-jam.github.io/battle-tested-patterns/zh/patterns/visitor/) — 树遍历分发 | **📊 验证来源** - React · Linux · Go - Redis · PostgreSQL - Kafka · Chromium - Tokio · Erlang/OTP - LevelDB · RocksDB · etcd - Nginx · Akka - LLVM · Vue · Godot - PyTorch · CPython · ZFS |
| 现有资源 | 缺失的部分 |
|---|---|
| 设计模式书 | 太抽象,过于 OOP |
| 算法仓库 | 脱离工程实践 |
| 系统设计指南 | 架构层面,不涉及代码级 |
本项目:从 React、Linux、Go、Chromium 等项目中提取的代码级技巧——每个都有可验证的源码链接。
| 模式 | 做什么 | 验证来源 |
|---|---|---|
| 位掩码 | 将 N 个标志打包到一个整数,O(1) 检查任意组合 | React Flags · Linux stat.h |
| 双缓冲 | 原子交换两份副本,零分配 | React Fiber · SDL |
| 协作调度 | 在工作块之间让出控制权保持响应 | React Scheduler · Go Runtime |
| 最小堆 | O(1) 查看最高优先级,O(log n) 插入/删除 | React MinHeap · Linux CFS |
| 差异/补丁 | 计算两个序列之间的最小编辑 | React Reconciler · Git |
| 对象池 | 预分配复用对象,避免 GC 压力 | Go sync.Pool · Godot |
| 环形缓冲区 | 固定大小循环队列,零分配 | LMAX Disruptor · Linux |
| 状态机 | 显式状态,不可能的转换不存在 | XState · Linux TCP |
| 写时复制 | 引用共享,修改时才复制 | Git objects · Rust Cow |
| 观察者 | 订阅事件,生产者消费者解耦 | Node EventEmitter · Redux |
| 迭代器 | 惰性序列,零中间分配 | Rust Iterator · Python gen |
| 信号量 | 计数器限制并发 | Linux · Go x/sync |
| 批处理 | 累积操作批量执行 | Kafka |
| 指数退避重试 | 指数延迟 + 抖动重试 | Kubernetes · gRPC |
| 事件循环 | 单线程循环通过 epoll/kqueue 复用 I/O | libuv · Redis ae |
| 享元/驻留 | 共享相同对象避免重复 | Python int cache |
| 布隆过滤器 | 概率集合成员测试,零漏判 | LevelDB · Chromium |
| 熔断器 | 停止调用故障服务,快速失败 | Hystrix · gobreaker |
| Arena 分配器 | 区域内推进指针分配,一次性释放 | bumpalo · Go arena |
| B+ 树 | 高扇出平衡树——内部节点引导,叶节点存储并链接用于范围扫描 | PostgreSQL · SQLite |
| 背压 | 消费者跟不上时减慢生产者 | Node.js Streams · [Reactive Streams](https://github.com/reactive-streams/reactive-streams-jvm/blob/a625d3aba756e9842ad |
$ claude mcp add battle-tested-patterns \
-- python -m otcore.mcp_server <graph>