搜索

PythonReactSEOPDF
GitHub Skill

design-pattern-decorator

Use when responsibilities must be added dynamically to objects without subclass proliferation.

zenobi-us☆ 39 仓库 Stars7 仓库 Forks

安装与使用

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

查看来源 ↗使用指南

技能说明

Skill: Decorator

Intent

Attach additional behavior to objects at runtime by wrapping them with decorator objects that preserve the same interface.

Applicability Signals

  • Signal 1: Feature combinations are expanding and subclass permutations are becoming unmanageable.
  • Signal 2: Optional responsibilities should be enabled/disabled per instance or per context.
  • Signal 3: You need to extend behavior while keeping original component code closed to modification.

Contraindications

  • Case 1: Only one static behavior variant exists; wrappers add unnecessary complexity.
  • Case 2: Object identity and strict type checks must remain concrete across boundaries.
  • Case 3: Deep wrapper chains would hurt latency/debuggability and cannot be controlled.

Decision Heuristics

  • If behavior should be layered dynamically and combinatorially, prefer Decorator.
  • If you need one global simplification entry point, Facade is often a better fit.
  • Decision anti-bias note: do not use decorators to hide missing cohesive domain abstraction.

Implementation Checklist

  • Define stable component interface used by both core and decorators.
  • Implement concrete core component.
  • Implement base decorator that delegates to wrapped component.
  • Add focused decorators, each responsible for one behavior increment.
  • Test ordering effects for stacked decorators and baseline passthrough.

Misuse Checks

  • Misuse 1: Decorator changes core contract semantics unexpectedly → Remediation: preserve interface guarantees and document any contract shifts.
  • Misuse 2: Too many decorators in unknown order → Remediation: define composition policy and builder/factory for valid stacks.
  • Misuse 3: Decorators become stateful mini-services → Remediation: extract long-lived state/services outside decorator chain.

Verification Rubric

  • Correctness:
    • Undecorated component behaves as baseline.
    • Each decorator adds behavior without breaking wrapped interface contract.
  • Design quality:
    • Decorator responsibilities are single-purpose and composable.
    • Wrapper order sensitivity is explicit and tested.
  • Regression safety:
    • Tests cover baseline, single decorator, and multi-decorator stacks.

Language-Specific Adaptations (Optional)

  • TypeScript: enforce component interface conformance on all decorators.
  • Python: explicit delegation methods keep wrapping behavior readable.
  • Go: interface-wrapping structs with embedded component references are straightforward.

Related Patterns (Optional)

  • Proxy: similar structure but intent is access control/lifecycle indirection, not feature layering.
  • Composite: builds tree structures; decorator builds wrapper chains.

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!