Flyweight

Implement the Flyweight pattern to share intrinsic state across similar objects.

65|15|Updated Mar 8, 2026
One-click install
npx skills add https://github.com/microwind/ai-skills --skill flyweight
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Flyweight
Source: https://github.com/microwind/ai-skills/tree/main/design-patterns/flyweight-pattern
Command: npx skills add https://github.com/microwind/ai-skills --skill flyweight

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve?

Flyweight pattern reduces memory usage by sharing intrinsic state across a large number of fine-grained objects, enabling scalable object creation and rendering.

Core Features & Use Cases

  • Shared intrinsic state to minimize memory footprint across millions of objects (e.g., font glyphs, icons, game particles).
  • Clear separation of intrinsic (shared) vs extrinsic (contextual) state to ensure safe reuse.
  • A lightweight factory or pool that reuses existing instances and prevents unnecessary duplication.

Quick Start

Create a FlyweightFactory and render many objects by supplying external state for each instance.

Frequently Asked Questions about Flyweight

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce memory usage when rendering millions of similar game sprites or UI icons?

To reduce memory usage for millions of similar objects like game sprites or UI icons, share their immutable intrinsic state across instances. This approach prevents unnecessary duplication by using a lightweight factory to reuse existing instances.

What is the difference between intrinsic and extrinsic state in memory optimization?

In memory optimization, intrinsic state is immutable and shared across objects to minimize footprint, while extrinsic state is contextual and passed externally. Clear separation ensures safe reuse without duplicating the core object data.

How do I implement object pooling for fine-grained objects in a text editor?

Implement object pooling by creating a FlyweightFactory to manage shared instances of fine-grained objects like font glyphs. Supply external state for each instance during rendering to reuse existing objects and prevent duplication.

When do I need the flyweight pattern for software design?

You need the flyweight pattern for software design when creating large numbers of fine-grained objects causes memory bottlenecks. It enables scalable object creation by sharing intrinsic state across scenarios like text characters or game particles.

Does memory optimization through shared objects work with mutable state?

Memory optimization through shared objects requires an immutable intrinsic state. If intrinsic state were mutable, changes would affect all shared instances. Contextual extrinsic state must be managed separately and passed externally during rendering.