mcpField
S
software-patterns

software-patterns

github.com/grndlvl开发1749 次安装38 周前更新
代码仓库

关于 software-patterns

面向软件工程问题的知识路由技能,将设计模式、算法、代码质量、分布式系统、领域建模和编程基础等内容导向相应技能。它也能为架构问题编排跨技能方案,并支持比较与实现类查询。

主要功能

  1. 01覆盖七类工程知识技能
  2. 02按问题路由到相关内容
  3. 03协调多技能架构方案
  4. 04提供模式与方案比较
  5. 05支持指定语言的实现查询

适用场景

  1. 01为具体问题选择设计模式
  2. 02按需求挑选数据结构与算法
  3. 03组合分布式系统架构概念
  4. 04比较实现方案及权衡

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/grndlvl/software-patterns --skill software-patterns

安装后在 Claude Code 中输入/software-patterns确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

examples/composite-solutions/
caching-example.md
event-system-example.md
index/
composite-solutions.yaml
problem-taxonomy.yaml
triggers.yaml
index/decision-trees/
data-structure-selection.yaml
pattern-selection.yaml
storage-engine-selection.yaml
quick-ref/
ds-cheatsheet.md
pattern-cheatsheet.md
templates/
language-idioms.md
pattern-implementation.md

详细介绍

软件模式 - 统一知识路由器

这是一个全面的软件工程知识库,涵盖 7 个专注领域中的 147 个文档文件。此路由器会智能地将查询引导至相应的底层技能,并在需要时协调跨技能解决方案。

此技能的触发时机

遇到以下情况时,此技能会自动触发:

  • 需要在设计模式或算法之间进行选择
  • 设计系统架构或数据模型
  • 讨论代码质量、重构或最佳实践
  • 根据特定需求选择数据结构
  • 设计分布式系统或数据库
  • 为复杂业务领域建模
  • 应用基础编程概念

快速参考:各项技能涵盖内容

技能文件数涵盖内容适用场景
gof-patterns2523 种 GoF 设计模式 + 选择指南对象创建、组合、行为
clrs-algorithms40数据结构与算法性能优化、算法选择
clean-code14SOLID 原则 + 8 项实践代码质量、重构、可维护性
ddia21分布式系统概念可扩展性、一致性、可用性
pragmatic-programmer197 项原则 + 11 项实践软件工艺、调试、工具
ddd154 种战略模式 + 5 种战术模式领域建模、限界上下文
sicp1312 项计算机科学基础概念抽象、递归、解释器

总计:147 个文档文件

查询命令

模式查询
/pattern <problem>

查找适用于特定问题的设计模式。

示例:

  • /pattern create objects without knowing exact type → 工厂方法
  • /pattern add behavior dynamically → 装饰器
  • /pattern notify multiple objects of changes → 观察者
  • /pattern simplify complex subsystem → 外观

路由至: 使用 pattern-selection.md 的 gof-patterns 技能

数据结构查询
/ds <requirement>

查找适用于特定需求的数据结构。

示例:

  • /ds fast lookup by key → 哈希表
  • /ds maintain sorted order → 树集或堆
  • /ds fast insert/delete at ends → 双端队列
  • /ds priority queue → 二叉堆

路由至: 使用 data-structure-selection.md 的 clrs-algorithms 技能

架构查询
/architecture <scenario>

获取结合模式、数据结构和分布式系统概念的多技能解决方案组合。

示例:

  • /architecture e-commerce checkout → 状态模式 + 命令模式 + 观察者模式 + 分布式事务
  • /architecture real-time leaderboard → 有序集合 + Redis + 发布/订阅
  • /architecture multi-tenant SaaS → 抽象工厂 + 限界上下文 + 分区

路由至: 协调 gof-patterns、clrs-algorithms、ddia 和 ddd 技能

实现查询
/implement <pattern> [language]

为特定语言中的某种模式生成实现代码。

示例:

  • /implement factory method typescript
  • /implement observer python
  • /implement heap java

路由至: 使用特定语言的转换能力,调用相应技能

比较查询
/compare <a> vs <b>

分析两种方案之间的权衡。

示例:

  • /compare factory method vs abstract factory
  • /compare array vs linked list
  • /compare postgres vs mongodb
  • /compare event sourcing vs crud

路由至: 调用相关技能并提供比较表

路由器的工作方式

1. 查询解析

路由器会分析查询中的问题指示词:

patterns:
  - "create", "instantiate", "build" → Creational patterns
  - "structure", "compose", "organize" → Structural patterns
  - "behavior", "algorithm", "interact" → Behavioral patterns

data_structures:
  - "fast lookup", "search", "find" → Hash or Tree
  - "sorted", "ordered" → Tree or Heap
  - "insert", "delete", "add", "remove" → List or Tree
  - "queue", "stack", "priority" → Specialized structures

distributed_systems:
  - "scale", "partition", "shard" → ddia/partitioning
  - "replicate", "consistency" → ddia/replication
  - "distributed", "consensus" → ddia/consensus

domain_modeling:
  - "entity", "value object", "aggregate" → ddd/tactical
  - "bounded context", "ubiquitous language" → ddd/strategic
2. 技能路由

根据查询类型,路由到一个或多个技能:

查询类型主要技能支持技能
设计模式gof-patternsclean-code(SOLID)、ddd(模式)
数据结构clrs-algorithmsddia(存储引擎)
代码质量clean-codepragmatic-programmer
分布式系统ddiaclrs-algorithms(图)、ddd(上下文)
领域建模dddgof-patterns(战术模式)
基础知识sicppragmatic-programmer
架构ALL编排式解决方案
3. 跨技能编排

对于复杂问题,路由器会编排多个技能:

示例:“为分布式系统设计缓存层”

1. clrs-algorithms → LRU cache data structure (Hash Table + Doubly Linked List)
2. gof-patterns → Proxy pattern (control access), Flyweight (share state)
3. ddia → Replication strategies, consistency models
4. clean-code → Interface design, SOLID principles

示例:“构建电商订单处理系统”

1. ddd → Order aggregate, bounded contexts (order, payment, shipping)
2. gof-patterns → State (order states), Command (payment actions), Observer (notifications)
3. clrs-algorithms → Priority Queue (order processing), Hash Table (inventory lookup)
4. ddia → Event sourcing, CQRS, distributed transactions
5. clean-code → SRP (one class per concern), DIP (depend on abstractions)

自动触发规则

当查询中包含以下指示词时,此技能会自动激活:

模式/设计指示词
  • “哪个模式”、“设计模式”、“我应该使用”
  • “工厂”、“单例”、“观察者”、“装饰器”、“适配器”
  • “创建对象”、“添加行为”、“简化接口”
数据结构指示词
  • “哪种数据结构”、“快速查找”、“排序顺序”
  • “数组”、“列表”、“树”、“哈希”、“图”、“堆”
  • “O(1)”、“O(log n)”、“复杂度”、“时间/空间”
架构指示词
  • “设计”、“架构”、“我应该如何构建”
  • “可扩展”、“分布式”、“高可用”
  • “微服务”、“事件驱动”、“领域模型”
代码质量指示词
  • “重构”、“代码异味”、“清理”、“改进”
  • “SOLID”、“DRY”、“命名”、“函数大小”
  • “测试”、“易于维护”、“可读”

技能覆盖范围详情

GoF 模式(gof-patterns)

创建型(5 种模式):

  • 抽象工厂、生成器、工厂方法、原型、单例

结构型(7 种):

  • 适配器、桥接、组合、装饰器、外观、享元、代理

行为型(11 种):

  • 责任链、命令、解释器、迭代器、中介者、备忘录、观察者、状态、策略、模板方法、访问者

决策指南:

  • pattern-selection.md - 全面的选择指南
  • 问题 → 模式映射
  • 常见组合
  • 应避免的反模式
CLRS 算法(clrs-algorithms)

线性结构(6 种):

  • 数组、动态数组、链表、栈、队列、双端队列

树(12 种):

  • 二叉树、BST、AVL、红黑树、B 树、字典树、堆、伸展树、Treap、区间树、顺序统计树、K-D 树

基于哈希的结构(3 种):

  • 哈希表、哈希集合、布隆过滤器

图(5 种):

  • 邻接表/矩阵、网络流、强连通分量,以及算法(BFS、DFS、Dijkstra 等)

高级结构(7 种):

  • 跳表、不相交集、线段树、芬威克树、斐波那契堆、二项堆、van Emde Boas 树

字符串(3 种):

  • 字符串算法(KMP、Rabin-Karp)、后缀数组、后缀树

算法:

  • 排序(QuickSort、MergeSort、HeapSort、RadixSort)

决策指南:

Clean Code(clean-code)

SOLID 原则(5 项):

  • 单一职责、开闭原则、里氏替换、接口隔离、依赖倒置

实践(8 项):

  • 有意义的命名、函数、注释、格式化、错误处理、单元测试、代码异味、童子军规则
DDIA (ddia)

数据模型(3):

  • 关系型、文档型、图

存储(3):

  • B 树、LSM 树、列式存储

复制(3):

  • 主从、多主、无主

分区(2):

  • 策略、再平衡

事务(3):

  • ACID、隔离级别、分布式事务

一致性(2):

  • 模型、线性一致性

共识(1):

  • 算法(Paxos、Raft)

处理(3):

  • 批处理、流处理、事件溯源/CQRS
Pragmatic Programmer (pragmatic-programmer)

原则(7):

  • DRY、正交性、可逆性、示踪弹(Tracer Bullets)、原型、领域语言、估算

实践(11):

  • 纯文本、Shell 技巧、调试、文本处理、代码生成器、契约式设计、断言式编程、解耦、重构、测试、自动化
Domain-Driven Design (ddd)

战略模式(4):

  • 通用语言、限界上下文、上下文映射、防腐层

战术模式(5):

  • 实体、值对象、聚合、领域服务、领域事件

辅助模式(3):

  • 仓储、工厂、规约

实践(2):

  • 事件风暴、模型探索
SICP (sicp)

过程(3):

  • 抽象、高阶函数、递归模式(线性、尾递归、树形、相互递归)

数据(3):

  • 数据抽象、层次化数据、符号数据

模块化(3):

  • 赋值与状态、环境模型、流

元语言(3):

  • 解释器、惰性求值、寄存器机器

跨技能解决方案栈

常见架构模式
1. 缓存栈
Pattern: Caching layer with eviction policy

Skills Used:
- clrs-algorithms: Hash Table + Doubly Linked List (LRU)
- gof-patterns: Proxy (control access), Flyweight (share state)
- ddia: Replication (distributed cache), Consistency (cache coherence)
- clean-code: SRP (separate concerns), DIP (interface-based)

Implementation Guide:
1. Use Hash Table for O(1) key lookup
2. Use Doubly Linked List for O(1) LRU eviction
3. Apply Proxy pattern to control access and logging
4. Apply Flyweight to share immutable state
5. Consider replication strategy for distributed scenarios
2. 事件管道
Pattern: Event-driven system with processing pipeline

Skills Used:
- gof-patterns: Observer (event notification), Command (encapsulate actions)
- clrs-algorithms: Queue (FIFO processing), Priority Queue (prioritized events)
- ddia: Stream Processing (Kafka/Flink), Event Sourcing
- ddd: Domain Events, Aggregates (event producers)
- clean-code: SRP (one handler per event type)

Implementation Guide:
1. Use Observer for event subscription
2. Use Queue or Priority Queue for event buffer
3. Use Command pattern for event handlers
4. Apply Event Sourcing for audit trail
5. Define Domain Events in Ubiquitous Language
3. 多租户 SaaS
Pattern: Isolated tenants with shared infrastructure

Skills Used:
- ddd: Bounded Contexts (per tenant or shared), Context Mapping
- gof-patterns: Abstract Factory (tenant-specific objects), Strategy (tenant policies)
- clrs-algorithms: Hash Table (tenant lookup), B-Tree (tenant data indexing)
- ddia: Partitioning (tenant sharding), Isolation Levels
- clean-code: OCP (extend without modifying), ISP (tenant-specific interfaces)

Implementation Guide:
1. Define Bounded Context boundaries (shared kernel vs separate)
2. Use Abstract Factory for tenant-specific object creation
3. Use Strategy for tenant-specific policies (pricing, limits)
4. Partition data by tenant ID for isolation
5. Choose appropriate isolation level (row-level, schema-level, database-level)
4. 电商订单系统
Pattern: Order processing with inventory, payment, shipping

Skills Used:
- ddd: Aggregates (Order, Inventory, Payment), Bounded Contexts
- gof-patterns: State (order lifecycle), Command (payment operations), Observer (notifications)
- clrs-algorithms: Priority Queue (order processing), Hash Table (inventory lookup)
- ddia: Distributed Transactions (Saga pattern), Event Sourcing
- clean-code: SRP (separate concerns), DIP (depend on repositories)

Implementation Guide:
1. Define Order Aggregate with invariants
2. Use State pattern for order states (cart → payment → processing → shipped)
3. Use Command for payment operations (process, refund, cancel)
4. Use Observer for notifications (customer, warehouse, shipping)
5. Apply Saga pattern for distributed transaction coordination
6. Use Event Sourcing for order history and replay
5. 实时排行榜
Pattern: Sorted rankings with fast updates

Skills Used:
- clrs-algorithms: Red-Black Tree or Skip List (sorted rankings), Hash Table (user lookup)
- gof-patterns: Observer (rank change notifications), Memento (snapshot rankings)
- ddia: Replication (read scaling), Partitioning (range-based)
- clean-code: Functions (small, focused), DRY (ranking logic)

Implementation Guide:
1. Use Sorted Set (Red-Black Tree) for O(log n) insert/delete/rank
2. Use Hash Table for O(1) user score lookup
3. Apply Observer for real-time rank change notifications
4. Use Read Replicas for scaling leaderboard queries
5. Partition by rank ranges for very large leaderboards

使用示例

示例 1:模式选择

查询:“我需要创建对象,但不指定其具体类。”

路由器响应:

Primary Match: Creational Patterns (gof-patterns)

Recommendation: Factory Method Pattern

Routing to: gof-patterns/gof-creational/factory-method.md

Additional Considerations:
- If creating families of related objects → Abstract Factory
- If construction is complex → Builder
- See: gof-patterns/pattern-selection.md for decision tree
示例 2:数据结构选择

查询:“哪种数据结构最适合快速查找并维持排序?”

路由器响应:

Primary Match: Data Structure Selection (clrs-algorithms)

Recommendation: Tree Map (Red-Black Tree or AVL Tree)

Comparison:
- Hash Table: O(1) lookup, NO sorted order
- Tree Map: O(log n) lookup, YES sorted order ✓
- Sorted Array: O(log n) search, O(n) insert ✗

Routing to: clrs-algorithms/data-structures/trees/red-black-tree.md

See also: clrs-algorithms/data-structure-selection.md
示例 3:架构设计

查询:“设计一个支持电子邮件、SMS 和推送通知的通知系统。”

路由器响应:

Multi-Skill Solution:

1. Pattern Selection (gof-patterns):
   - Observer Pattern: Subscribe to events
   - Strategy Pattern: Switch between notification methods
   - Command Pattern: Queue notification actions

2. Data Structures (clrs-algorithms):
   - Priority Queue: Prioritize urgent notifications
   - Hash Table: User preference lookup

3. Distributed Systems (ddia):
   - Queue (Kafka/RabbitMQ): Async processing
   - Retry mechanisms: Handle failures

4. Code Quality (clean-code):
   - SRP: One class per notification channel
   - OCP: Add new channels without modifying existing

Implementation Stack:
Observer → Command → Priority Queue → Strategy → DIP
示例 4:代码质量审查

查询:“这个函数有 200 行,做了太多事情。我该如何改进?”

路由器响应:

Primary Match: Code Quality (clean-code)

Violations Detected:
1. Single Responsibility Principle (SRP) - function does too many things
2. Function size - should be 5-20 lines

Refactoring Steps:
1. Extract methods for each responsibility (clean-code/practices/functions.md)
2. Apply SRP (clean-code/solid/single-responsibility.md)
3. Use meaningful names (clean-code/practices/meaningful-names.md)

Supporting Guidance:
- pragmatic-programmer/practices/refactoring.md: When and how to refactor
- gof-patterns: Consider Template Method or Strategy if algorithm varies

Tools:
- Extract Method refactoring
- Compose Method pattern
- Replace Temp with Query

语言翻译说明

所有示例均使用与语言无关的伪代码,并遵循以下约定:

  • 使用 class 定义类型
  • 使用 function 表示方法/函数
  • 使用 -> 表示对象上的方法调用
  • 使用 // 表示注释
  • 类型提示的写法为 name: Type
翻译指南
语言类方法调用注释类型
PHPclassfunction->//文档块或 PHP 8+
JavaScriptclassfunction / 箭头函数.//JSDoc 或 TypeScript
TypeScriptclass方法 / 箭头函数.//原生类型
Pythonclassdef.#类型提示
Javaclass方法.//原生类型
C#class方法.//原生类型
Gotype / structfunc.//原生类型
Ruststruct / traitfn.//原生类型

高级用法

组合多项技能

对于复杂问题,请明确请求多技能分析:

"I need a comprehensive solution for [problem] covering patterns, data structures, and distributed systems"

路由器将协调所有相关技能,并提供:

  1. 模式建议(gof-patterns)
  2. 数据结构选择(clrs-algorithms)
  3. 可扩展性考量(ddia)
  4. 领域建模(如适用,则使用 ddd)
  5. 代码质量指南(clean-code)
  6. 实现最佳实践(pragmatic-programmer)
深入探讨

请求特定技能的详细文档:

"Show me the full Observer pattern documentation"
→ Routes to: gof-patterns/gof-behavioral/observer.md

"Explain Red-Black Tree implementation with examples"
→ Routes to: clrs-algorithms/data-structures/trees/red-black-tree.md

"What are all SOLID principles?"
→ Routes to: clean-code/solid/ (all 5 principles)
对比查询

请求权衡分析:

"/compare singleton vs dependency injection"
→ Multi-skill analysis from gof-patterns + clean-code

"/compare b-tree vs lsm-tree"
→ Multi-skill analysis from clrs-algorithms + ddia

"/compare entity vs value object"
→ Analysis from ddd/tactical/

高效使用技巧

1. 从问题出发,而非解决方案

❌ “给我展示单例模式” ✅ “我需要配置管理器只有一个实例”

路由器会推荐合适的模式,并提醒你注意潜在问题。

2. 提供上下文

❌ “我该使用哪种数据结构?” ✅ “我需要按键快速查找,并能对 10,000 个项目进行排序迭代”

上下文有助于更好地路由和提供建议。

3. 询问权衡取舍

✅ “工厂方法与抽象工厂之间有哪些权衡?” ✅ “我应该在什么时候使用数组,而不是链表?” ✅ “比较事件溯源与传统 CRUD”

有关权衡取舍的查询会触发对比模式,并提供表格和决策指南。

4. 请求实现指导

✅ “如何用 TypeScript 实现 LRU 缓存?” ✅ “用 Python 展示观察者模式” ✅ “用 PHP 实现仓储模式”

包含针对特定语言的代码生成,并遵循最佳实践。

5. 探索相关概念

获得建议后,可以询问:

  • “哪些模式适合与 [模式] 搭配使用?”
  • “[数据结构] 常见的组合有哪些?”
  • “[概念] 与 [其他概念] 有何关联?”

贡献

要向任意技能添加新模式、算法或概念,请:

  1. 遵循现有文档中确立的格式
  2. 包含定义、适用场景、实现、示例和权衡取舍
  3. 更新相关 SKILL.md 中的快速参考表
  4. 如适用,添加决策指南条目

致谢

这个统一知识库建立在众多先驱的成果之上:

  • 四人帮(Gamma、Helm、Johnson、Vlissides):设计模式
  • CLRS(Cormen、Leiserson、Rivest、Stein):算法导论
  • Robert C. Martin(Uncle Bob):代码整洁之道
  • Martin Kleppmann:设计数据密集型应用
  • Eric Evans:领域驱动设计
  • Andrew Hunt & David Thomas:程序员修炼之道
  • Harold Abelson & Gerald Jay Sussman:计算机程序的构造和解释

使用 Claude Code 制作

总计:7 个专注型技能中的 147 个文档文件

评论

支持 Markdown