搜索

PythonReactSEOPDF
GitHub Skill

design-pattern-flyweight

Use when huge numbers of similar objects cause memory pressure and shared intrinsic state can be externalized.

zenobi-us☆ 39 仓库 Stars7 仓库 Forks

安装与使用

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

查看来源 ↗使用指南

技能说明

Skill: Flyweight

Intent

Reduce memory usage by sharing common intrinsic state across many fine-grained objects while keeping varying extrinsic state outside the shared instances.

Applicability Signals

  • Signal 1: Profiling shows excessive memory from many nearly identical objects.
  • Signal 2: Object state can be clearly split into shared (intrinsic) and context-specific (extrinsic) parts.
  • Signal 3: Object creation/destruction churn is high and cacheable shared instances are practical.

Contraindications

  • Case 1: Object count is low and memory pressure is not material.
  • Case 2: State is mostly unique per object and cannot be cleanly externalized.
  • Case 3: Concurrency/lifecycle complexity of sharing would outweigh memory savings.

Decision Heuristics

  • If high cardinality objects share large immutable state, prefer Flyweight.
  • If you need identity/lifecycle control rather than memory sharing, Proxy or object pools may fit better.
  • Decision anti-bias note: do not apply flyweight without measurements showing a real memory bottleneck.

Implementation Checklist

  • Separate intrinsic state from extrinsic runtime context.
  • Make intrinsic state immutable or safely shared.
  • Introduce flyweight factory/cache keyed by intrinsic state.
  • Refactor clients to pass extrinsic state at call time.
  • Benchmark memory/performance before and after.

Misuse Checks

  • Misuse 1: Extrinsic state accidentally stored in shared flyweight → Remediation: enforce immutable intrinsic-only flyweights.
  • Misuse 2: Factory cache grows unbounded → Remediation: add eviction/lifecycle strategy.
  • Misuse 3: No measurable gain after complexity increase → Remediation: remove flyweight and simplify model.

Verification Rubric

  • Correctness:
    • Shared flyweight behavior remains correct across diverse extrinsic contexts.
    • Factory returns shared instance for equivalent intrinsic keys.
  • Design quality:
    • Intrinsic/extrinsic boundary is explicit and documented.
    • Sharing does not introduce hidden mutable-state coupling.
  • Regression safety:
    • Tests cover key reuse, context variation, and cache behavior edge cases.

Language-Specific Adaptations (Optional)

  • TypeScript: use map-based factories with readonly intrinsic payloads.
  • Python: consider functools.lru_cache or explicit registries for immutable flyweights.
  • Go: use map+sync strategy appropriate for concurrent access patterns.

Related Patterns (Optional)

  • Singleton: one shared instance globally; flyweight shares many keyed instances.
  • Proxy: controls access/lifecycle, while flyweight primarily optimizes memory footprint.

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!