← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Flyweight Pattern

quality Advanced
debt(d7/e5/b5/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list php-meminfo and blackfire as tools, both specialist profiling tools requiring deliberate instrumentation and measurement. The automated flag is 'no', meaning no linter or static analysis catches misuse automatically. Memory bloat from missing flyweight only manifests at scale (thousands of objects), making it invisible in normal development and only apparent under profiling or when users hit OOM errors in production.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix says to share identical immutable objects from a factory instead of creating new ones. This requires introducing a factory class, making flyweight objects immutable, identifying all creation sites, and threading the factory through client code — not a one-liner but also not a full architectural rework. Common mistakes (mutable flyweights, missing factory, extrinsic state leakage) suggest several interrelated changes across a component.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). The pattern applies to web, cli, and queue-worker contexts, and once introduced, every future maintainer must understand the intrinsic/extrinsic state split, the factory contract, and the immutability invariant. Flyweight imposes an ongoing cognitive tax on all code touching the shared objects, though it doesn't reshape the entire system's architecture.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field states developers believe flyweight is only for extremely large systems, missing that thousands of repeated UI components in ordinary PHP apps qualify. Common mistakes reinforce multiple traps: storing extrinsic state in the flyweight (defeats sharing), mutating shared objects (corrupts all contexts), and skipping the factory (creates duplicates). These are documented gotchas that are non-obvious but learnable, not catastrophic inversions of intuition.

About DEBT scoring →

Also Known As

flyweight object pool shared state

TL;DR

A structural pattern that shares common state between many objects rather than storing it in each instance — dramatically reducing memory for large numbers of similar objects.

Explanation

Flyweight separates intrinsic state (shared, immutable — stored in the flyweight) from extrinsic state (unique, context-dependent — passed by the client). A flyweight factory ensures each unique intrinsic state is only instantiated once. Classic example: a text editor storing character formatting — rather than one object per character with its font stored on each, share font objects across all characters using the same font. PHP applications: caching expensive configuration objects, connection pools, icon/resource caches.

Common Misconception

Flyweight is only relevant for extremely large systems — any PHP application rendering thousands of repeated components (table rows, list items with shared formatters) benefits from flyweight.

Why It Matters

Creating one object per rendered row in a 10,000-row table with shared formatters uses 10,000× more memory than flyweight — the pattern is the difference between workable and out-of-memory.

Common Mistakes

  • Storing extrinsic (context-specific) state in the flyweight — defeats the purpose by making it non-shareable.
  • Flyweight objects that are mutable — shared objects must be immutable; mutation in one context corrupts all others.
  • Not using a factory — without a factory, client code may create duplicates instead of sharing.
  • Premature flyweight optimisation — measure memory usage first; flyweight adds complexity only justified by real memory pressure.

Code Examples

✗ Vulnerable
// One object per row — wasteful if formatter is identical:
class TableRow {
    private NumberFormatter $formatter;
    public function __construct(private array $data) {
        $this->formatter = new NumberFormatter('en-US', NumberFormatter::CURRENCY);
        // New formatter for every row — 10,000 rows = 10,000 identical formatters
    }
}
✓ Fixed
// Flyweight — shared formatter:
class FormatterFactory {
    private static array $cache = [];
    public static function get(string $locale, int $style): NumberFormatter {
        $key = $locale . ':' . $style;
        return self::$cache[$key] ??= new NumberFormatter($locale, $style);
    }
}

class TableRow {
    public function __construct(private array $data) {}
    public function formatAmount(): string {
        // Shared — created once, reused by all rows:
        return FormatterFactory::get('en-US', NumberFormatter::CURRENCY)
            ->formatCurrency($this->data['amount'], 'USD');
    }
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 1 ping W 2 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 6 Perplexity 3 Unknown AI 2 Google 2 ChatGPT 2 Majestic 1 Ahrefs 1
crawler 15 crawler_json 2
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: High
⚡ Quick Fix
Share identical immutable objects from a factory instead of creating new ones — PHP's interned strings already do this; apply flyweight when you have thousands of objects with shared state
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Thousands of objects with identical shared configuration; Color or Font objects created per character in document rendering
Auto-detectable: ✗ No php-meminfo blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant