搜索

PythonReactSEOPDF
GitHub Skill

design-pattern-adapter

Use when an existing class has useful behavior but an incompatible interface blocks integration with client code.

zenobi-us☆ 39 仓库 Stars7 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Skill: Adapter

Intent

Convert one interface into another interface expected by clients so existing functionality can be reused without rewriting either side immediately.

Applicability Signals

  • Signal 1: A third-party or legacy component provides needed behavior but exposes mismatched method names/signatures.
  • Signal 2: Client code expects a stable contract and cannot absorb provider-specific differences.
  • Signal 3: Multiple integrations repeat small translation shims, creating duplicated glue code.

Contraindications

  • Case 1: You own both sides and can safely change one interface directly.
  • Case 2: Mismatch is temporary and can be removed faster by small targeted refactor.
  • Case 3: Adapter would hide severe semantic differences, not just interface differences.

Decision Heuristics

  • If interface incompatibility is the core blocker while behavior is otherwise acceptable, prefer Adapter.
  • If you need to simplify a whole subsystem API, Facade is often a better fit.
  • Decision anti-bias note: avoid stacking adapters to postpone necessary domain model cleanup.

Implementation Checklist

  • Define/confirm the target interface clients already rely on.
  • Wrap the adaptee in an adapter that implements the target interface.
  • Translate method names, argument shapes, and return formats explicitly.
  • Centralize validation/error mapping at adapter boundaries.
  • Add tests for both normal and mismatch/error translation paths.

Misuse Checks

  • Misuse 1: Adapter adds business logic unrelated to translation → Remediation: move business rules back into domain/service layer.
  • Misuse 2: Client leaks adaptee types through adapter API → Remediation: keep API strictly on target interface types.
  • Misuse 3: Chain of adapters accumulates for one workflow → Remediation: consolidate into one explicit mapping boundary or refactor contract.

Verification Rubric

  • Correctness:
    • Client code can call target interface without direct adaptee knowledge.
    • Adapter correctly maps input/output and error semantics.
  • Design quality:
    • Translation concerns are isolated in adapter, not scattered in clients.
    • Target contract remains stable and readable.
  • Regression safety:
    • Tests cover baseline flow and at least one edge-case translation path.

Language-Specific Adaptations (Optional)

  • TypeScript: use interface-based adapter classes; add runtime guards when external payloads are untyped.
  • Python: prefer composition over inheritance; use protocols for target contract clarity.
  • Go: implement target interface on adapter struct wrapping adaptee.

Related Patterns (Optional)

  • Facade: simplifies complex subsystem usage; adapter focuses on interface compatibility.
  • Bridge: separates abstraction and implementation when both should vary independently.

Attribution & Sources

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-03-11
索引更新时间
2026-03-11
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!