帮助用户用接口封装实现细节,从业务层面设计与理解系统
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "Encapsulating Complexity" 技能: 1. 下载 https://raw.githubusercontent.com/obra/clank/main/skills/architecture/encapsulating-complexity/SKILL.md 2. 保存为 ~/.claude/skills/encapsulating-complexity/SKILL.md 3. 装好后重载技能,告诉我可以用了
请根据“封装复杂性”的原则,帮我重构这个支付模块设计。目标是对外只暴露业务接口,隐藏数据库、第三方 API 和重试逻辑。请给出接口定义、职责划分和重构建议。
一份以业务接口为核心的模块重构方案,包含抽象接口与实现隔离建议。
把这个复杂的推荐系统方案改写成面向产品经理的说明,重点描述“系统做什么”,不要展开“内部怎么实现”。请用业务语言、模块接口和输入输出说明。
一份弱化技术细节、突出业务能力和接口职责的方案说明。
我在设计一个用户通知服务,请基于“隐藏实现细节、暴露稳定接口”的思路,提出 API 设计建议。请区分公开接口、内部实现、可替换依赖和扩展点。
一套强调稳定抽象、低耦合和可扩展性的服务接口设计建议。
Hide HOW things work. Expose only WHAT they do. Work at domain level (Users, Orders, Config), not implementation level (dicts, SQL rows, JSON files).
Core principle: The point of encapsulation is to create possibilities (many ways to implement) and restrict possibilities (one way to use). Implementation details hidden = free to change implementation without breaking clients.
Violating the letter of this rule is violating the spirit of information hiding.
Apply to every class and interface:
Warning signs you're violating encapsulation:
Ask these questions about every public method/field:
Does the interface expose HOW or WHAT?
Can I change implementation without breaking clients?
Do clients work at domain level or implementation level?
user.email, config.get_timeout()row[2], json_data['timeout_ms']Do return values expose internals?
User objectdict with database column namesIf answers reveal implementation details → encapsulation violated.
class ConfigManager:
def __init__(self, json_path):
self.json_path = json_path # ❌ Exposes JSON
self._data = {}
def get_value(self, key):
return self._data.get(key) # ⚠️ Returns raw value
def save_to_json(self): # ❌ "JSON" in method name
with open(self.json_path, 'w') as f:
json.dump(self._data, f)
Client code:
config = ConfigManager("/path/to/config.json") # Must know it's JSON
timeout = config.get_value("timeout_ms") # Must know exact key format
config.save_to_json() # Tied to JSON format
If you switch JSON → YAML: Client code breaks. Method names wrong. Constructor signature wrong.
class Config:
def __init__(self, config_file): # ✅ No format specified
self._storage = self._load(config_file) # ✅ Implementation hidden
def get_timeout(self): # ✅ Domain method, not raw key access
return self._storage.get("timeout_ms", 5000)
def set_timeout(self, seconds): # ✅ Domain operation
self._storage["timeout_ms"] = seconds * 1000 # ms internally
def save(self): # ✅ No "JSON" in name
self._persist(self._storage)
def _load(self, config_file): # ✅ Private - can change format
# Could load JSON, YAML, TOML, etc.
pass
def _persist(self, data): # ✅ Private - implementation detail
# Format hidden from clients
pass
Client code:
config = Config("app.config") # Format agnostic
timeout = config.get_timeout() # Domain method (seconds)
config.set_timeout(10)
config.save()
Switch JSON → YAML: Client code unchanged. Just change _load() and _persist().
Work at the problem domain, not the solution domain:
❌ Implementation Level:
class UserManager:
def get_user_row(self, user_id):
…
先用伪代码梳理方案与迭代思路,再高效转成可执行代码。
帮助开发者用早返回或表驱动方式简化嵌套条件分支,提升代码可读性。
帮助你为变量选择清晰准确、易维护的命名,提升代码可读性。
帮助开发者保持类接口抽象一致,避免混杂序列化、持久化等无关职责。
帮助你撰写不过时的代码注释,聚焦做什么与为什么而非时序背景。
帮助用户检索过往 Claude Code 对话,快速找回事实、决策与上下文线索。
帮助开发者按业务领域为代码命名,提升可读性与协作一致性。
帮助用户识别并降低软件系统复杂度,提升可维护性与开发效率。
帮助你识别职责混杂的例程,并拆分为单一职责的清晰结构。
帮助你从间距、字体、阴影与交互细节入手,系统提升界面精致感。
帮助开发者为代码补充设计意图注释,聚焦原因与关键取舍而非表面功能。
帮助开发者设计、实现并重构六边形架构系统,确保边界清晰且易于测试。