design-an-interface
- 信任分
- 88/100
- 兼容 Agent
- 1
- 领域
- 设计与多媒体
- 兼容 Agent
- Claude Code
- 信任分
- 88 / 100 · 社区维护
- 作者 / 版本 / 许可
- @mattpocock · 未声明 license
- 安装命令数
- 1 条
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
想读作者英文原文? ↓ 滚到正文区切换 · 在 GitHub 查看 ↗
设计思路
作者把「设计一个接口」做成一个多 sub-agent 并行探索 + 用户对比的工作流,灵感直接来自《A Philosophy of Software Design》。核心信念:好接口不是想出来的,是比出来的——并行让 3 个以上 sub-agent 各自带不同约束设计同一个模块,把差异最大的几份摆到一起,让用户在对比中看到取舍,而不是看一份「最优解」。
工作流
1. 收集需求:先问清楚模块用途、调用者、约束条件、成功标准。
2. 并行生成多份设计:用 Task tool 同时 spawn ≥3 个 sub-agent,每个分一个互斥的约束:
- Agent 1:方法数最少(1-3 个)
- Agent 2:最大灵活,覆盖很多 use case
- Agent 3:为最常见 case 优化
- Agent 4:从某个具体范式 / 库(比如 actor model、Stripe SDK)汲取灵感
每个 sub-agent 必须产出:接口签名 / 调用示例 / 内部隐藏了什么 / 这个设计的取舍。
3. 顺序呈现设计:让用户逐个看完每份后再做对比,不要一次糊脸塞。
4. 对比设计:比较维度——
- Interface simplicity:方法数 / 参数复杂度
- General-purpose vs Specialized:通用 vs 专用
- Implementation efficiency:接口形状是否允许高效实现
- Depth:小接口藏大复杂度(深模块,好)vs 大接口包薄实现(浅模块,避)
- Ease of correct use vs ease of misuse
用文字讨论取舍,不要做表格——表格会把差异平均化。
5. 综合:往往最好的方案是糅合多份的洞察。问用户:「哪份最贴你的主用例?其他设计里有什么值得移植的?」
反模式
- 别让 sub-agent 产出相似设计——必须强制激进差异化
- 别跳过对比——价值就在对比里
- 别直接落地——这一步只是接口形状
- 别按实现工作量来评估——这一步不衡量实现成本
适合谁
- 在写库 / SDK / 内部模块、重要边界要立得住的工程师
- 团队里 review 接口稳定性的资深成员
- 写第二版时知道第一版接口该重做的人
何时不该用
- 改个内部辅助函数——不需要这套形式
- 已经有强约束的接口(外部协议、合同 API)——形状是固定的
配套
brainstorming(先把要做什么搞清楚)、writing-plans(接口定下来后写实施)、request-refactor-plan(要替换旧接口时)。
Design an Interface
Based on "Design It Twice" from "A Philosophy of Software Design": your first idea is unlikely to be the best. Generate multiple radically different designs, then compare.
Workflow
1. Gather Requirements
Before designing, understand:
- What problem does this module solve?
- Who are the callers? (other modules, external users, tests)
- What are the key operations?
- Any constraints? (performance, compatibility, existing patterns)
- What should be hidden inside vs exposed?
Ask: "What does this module need to do? Who will use it?"
2. Generate Designs (Parallel Sub-Agents)
Spawn 3+ sub-agents simultaneously using Task tool. Each must produce a radically different approach.
Prompt template for each sub-agent:
Design an interface for: [module description]
Requirements: [gathered requirements]
Constraints for this design: [assign a different constraint to each agent]
- Agent 1: "Minimize method count - aim for 1-3 methods max"
- Agent 2: "Maximize flexibility - support many use cases"
- Agent 3: "Optimize for the most common case"
- Agent 4: "Take inspiration from [specific paradigm/library]"
Output format:
1. Interface signature (types/methods)
2. Usage example (how caller uses it)
3. What this design hides internally
4. Trade-offs of this approach
3. Present Designs
Show each design with:
- Interface signature - types, methods, params
- Usage examples - how callers actually use it in practice
- What it hides - complexity kept internal
Present designs sequentially so user can absorb each approach before comparison.
4. Compare Designs
After showing all designs, compare them on:
- Interface simplicity: fewer methods, simpler params
- General-purpose vs specialized: flexibility vs focus
- Implementation efficiency: does shape allow efficient internals?
- Depth: small interface hiding significant complexity (good) vs large interface with thin implementation (bad)
- Ease of correct use vs ease of misuse
Discuss trade-offs in prose, not tables. Highlight where designs diverge most.
5. Synthesize
Often the best design combines insights from multiple options. Ask:
- "Which design best fits your primary use case?"
- "Any elements from other designs worth incorporating?"
Evaluation Criteria
From "A Philosophy of Software Design":
Interface simplicity: Fewer methods, simpler params = easier to learn and use correctly.
General-purpose: Can handle future use cases without changes. But beware over-generalization.
Implementation efficiency: Does interface shape allow efficient implementation? Or force awkward internals?
Depth: Small interface hiding significant complexity = deep module (good). Large interface with thin implementation = shallow module (avoid).
Anti-Patterns
- Don't let sub-agents produce similar designs - enforce radical difference
- Don't skip comparison - the value is in contrast
- Don't implement - this is purely about interface shape
- Don't evaluate based on implementation effort