Awesome-claude-code create-flyweight
Generates Flyweight pattern for PHP 8.4. Optimizes memory via shared intrinsic state. Includes unit tests.
install
source · Clone the upstream repo
git clone https://github.com/dykyi-roman/awesome-claude-code
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/dykyi-roman/awesome-claude-code "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/create-flyweight" ~/.claude/skills/dykyi-roman-awesome-claude-code-create-flyweight && rm -rf "$T"
manifest:
skills/create-flyweight/SKILL.mdsource content
Flyweight Pattern Generator
Creates Flyweight pattern infrastructure for memory optimization through object sharing.
When to Use
| Scenario | Example |
|---|---|
| Large number of similar objects | Icons, glyphs, particles |
| Memory constraints | Mobile apps, embedded systems |
| Immutable shared state | Currency codes, tax rates |
| Performance optimization | Reduce object creation overhead |
Component Characteristics
Flyweight Interface
- Defines operations
- Accepts extrinsic state as parameters
ConcreteFlyweight
- Stores intrinsic state (shared)
- Immutable
- Reusable across contexts
FlyweightFactory
- Creates and manages flyweights
- Returns existing or new flyweight
- Ensures sharing
Generation Process
Step 1: Generate Flyweight Interface
Path:
src/Domain/{BoundedContext}/
— Operations contract{Name}Interface.php
Step 2: Generate ConcreteFlyweight
Path:
src/Domain/{BoundedContext}/
— Shared object{Name}Flyweight.php
Step 3: Generate FlyweightFactory
Path:
src/Domain/{BoundedContext}/Factory/ or src/Infrastructure/
— Manages flyweights{Name}FlyweightFactory.php
Step 4: Generate Tests
— Flyweight behavior and sharing verification{ClassName}Test.php
File Placement
| Component | Path |
|---|---|
| Flyweight Interface | |
| ConcreteFlyweight | |
| FlyweightFactory | |
| Unit Tests | |
Naming Conventions
| Component | Pattern | Example |
|---|---|---|
| Flyweight Interface | | |
| ConcreteFlyweight | | |
| FlyweightFactory | | |
| Test | | |
Quick Template Reference
Flyweight
final readonly class {Name}Flyweight implements {Name}Interface { public function __construct( private string $intrinsicState ) {} public function {operation}(string $extrinsicState): {returnType} { return {combine intrinsic and extrinsic state}; } }
FlyweightFactory
final class {Name}FlyweightFactory { private array $flyweights = []; public function getFlyweight(string $key): {Name}Interface { if (!isset($this->flyweights[$key])) { $this->flyweights[$key] = new {Name}Flyweight($key); } return $this->flyweights[$key]; } public function getCount(): int { return count($this->flyweights); } }
Usage Example
$factory = new CurrencyFlyweightFactory(); // Same object returned $usd1 = $factory->getFlyweight('USD'); $usd2 = $factory->getFlyweight('USD'); assert($usd1 === $usd2); // true // Format with extrinsic state $usd1->format(100.50); // "$100.50"
Common Flyweights
| Flyweight | Purpose |
|---|---|
| CurrencyFlyweight | Currency codes and symbols |
| IconFlyweight | UI icons |
| TaxRuleFlyweight | Tax rates by region |
| CharacterFlyweight | Text rendering glyphs |
| ColorFlyweight | Color palettes |
Anti-patterns to Avoid
| Anti-pattern | Problem | Solution |
|---|---|---|
| Mutable Flyweight | State changes affect all users | Make flyweights immutable |
| No Factory | Manual object management | Use flyweight factory |
| Large Intrinsic State | Memory not optimized | Keep intrinsic state minimal |
| Extrinsic in Flyweight | Not reusable | Pass extrinsic via parameters |
| Premature Optimization | Complexity without benefit | Profile first |
References
For complete PHP templates and examples, see:
— Flyweight, factory templatesreferences/templates.md
— Currency, icon, tax rule flyweights with unit testsreferences/examples.md