From acc
Generates Flyweight pattern for PHP 8.4 to optimize memory usage by sharing intrinsic state across similar objects like icons or glyphs. Includes interface, factory, and unit tests.
How this skill is triggered — by the user, by Claude, or both
Slash command
/acc:create-flyweightThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Creates Flyweight pattern infrastructure for memory optimization through object sharing.
Creates Flyweight pattern infrastructure for memory optimization through object sharing.
| 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 |
Path: src/Domain/{BoundedContext}/
{Name}Interface.php — Operations contractPath: src/Domain/{BoundedContext}/
{Name}Flyweight.php — Shared objectPath: src/Domain/{BoundedContext}/Factory/ or src/Infrastructure/
{Name}FlyweightFactory.php — Manages flyweights{ClassName}Test.php — Flyweight behavior and sharing verification| Component | Path |
|---|---|
| Flyweight Interface | src/Domain/{BoundedContext}/ |
| ConcreteFlyweight | src/Domain/{BoundedContext}/ |
| FlyweightFactory | src/Domain/{BoundedContext}/Factory/ |
| Unit Tests | tests/Unit/Domain/{BoundedContext}/ |
| Component | Pattern | Example |
|---|---|---|
| Flyweight Interface | {Name}Interface | CurrencyInterface |
| ConcreteFlyweight | {Name}Flyweight | CurrencyFlyweight |
| FlyweightFactory | {Name}FlyweightFactory | CurrencyFlyweightFactory |
| Test | {ClassName}Test | CurrencyFlyweightTest |
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};
}
}
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);
}
}
$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"
| Flyweight | Purpose |
|---|---|
| CurrencyFlyweight | Currency codes and symbols |
| IconFlyweight | UI icons |
| TaxRuleFlyweight | Tax rates by region |
| CharacterFlyweight | Text rendering glyphs |
| ColorFlyweight | Color palettes |
| 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 |
For complete PHP templates and examples, see:
references/templates.md — Flyweight, factory templatesreferences/examples.md — Currency, icon, tax rule flyweights with unit testsnpx claudepluginhub dykyi-roman/awesome-claude-code --plugin accUse when an application creates a very large number of fine-grained objects whose combined memory cost is prohibitive — share intrinsic state and store extrinsic state externally.
Generates Composite pattern for PHP 8.4 with interface, leaf, composite classes, and unit tests. For tree structures like menus, file systems, org charts.
Shares fine-grained objects to reduce memory usage by separating intrinsic and extrinsic state. Use when profiling shows many similar objects causing memory or GC pressure.