flyweight-pattern — скилл для Claude Code
Скилл flyweight-pattern учит агента применять паттерн приспособленец для экономии памяти при создании множества похожих объектов.
MIT 634 установок ★ 259 обновлён 2026-04-15
npx skills add PatternsDev/skills --skill flyweight-pattern -a claude-codeЧто делает flyweight-pattern
Скилл объясняет, как разделить состояние объекта на внутреннее (общее) и внешнее (индивидуальное). Общие части кэшируются в Map и переиспользуются, поэтому вместо сотен одинаковых экземпляров создаётся один. В примере из скилла библиотека хранит три книги, хотя на полке стоит пять экземпляров: две из них — копии с тем же ISBN.
Внутри — инструкция и разбор кода на JavaScript. Функция createBook проверяет, есть ли книга с таким ISBN в Map, и возвращает существующий экземпляр вместо нового. Функция addBook добавляет к нему внешние поля — доступность и количество продаж.
Скилл сразу оговаривает, когда паттерн не нужен. Если объектов мало или общих свойств почти нет, разделение состояния добавит сложности без выигрыша. В JavaScript часто проще обойтись прототипным наследованием. Современное железо с гигабайтами памяти тоже снижает ценность приёма, поэтому применять его стоит осознанно.
Какие задачи решает
- сократить расход RAM при массовом создании объектов
- переиспользовать экземпляры с одинаковым ISBN
- разделить общее состояние книги и данные конкретного экземпляра
- объяснить агенту, когда приспособленец не оправдан
- научить агента кэшировать объекты через Map
Примеры запросов агенту
- Примени паттерн приспособленец к моему классу
Book, чтобы одинаковые ISBN не плодили новые экземпляры - Покажи, как переписать
createBookс кэшем черезMap - Объясни, стоит ли здесь разделять состояние на внутреннее и внешнее
Как установить flyweight-pattern
1Через Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill flyweight-pattern -a claude-code2Вручную: положить файлы
~/.claude/skills/flyweight-pattern/SKILL.mdПапка скилла целиком; для одного проекта — .claude/skills/flyweight-pattern/
1Через Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill flyweight-pattern -a codex2Вручную: положить файлы
.agents/skills/flyweight-pattern/SKILL.mdДля всех проектов — ~/.agents/skills/
1Через Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill flyweight-pattern -a cursor2Вручную: положить файлы
.cursor/skills/flyweight-pattern/SKILL.mdCursor читает и .agents/skills/, и .claude/skills/
1GitHub CLI
gh skill install PatternsDev/skills javascript/flyweight-pattern/SKILL.md2Вручную: положить файлы
.github/skills/flyweight-pattern/SKILL.md1Через Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill flyweight-pattern -a gemini-cli2Командой платформы
gemini skills install https://github.com/PatternsDev/skills --path javascript/flyweight-patternВручную: положить файлы
.devin/skills/flyweight-pattern/SKILL.mdСтарый путь .windsurf/skills/ тоже читается
Через Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill flyweight-pattern -a opencodeClaude.ai
Упакуйте папку в zip и загрузите в Settings → Capabilities → Skills
По шагам, с командами для каждой платформы: Как установить скилл в Claude Code: пять способов
Вопросы о flyweight-pattern
Как установить flyweight-pattern в Claude Code?
Выполните npx skills add PatternsDev/skills --skill flyweight-pattern -a claude-code. Файл скилла появится в ~/.claude/skills/flyweight-pattern/SKILL.md.
Что делает скилл flyweight-pattern?
Учит агента применять паттерн приспособленец: общие свойства объектов выносятся в кэш, а индивидуальные хранятся отдельно. Это снижает расход памяти при большом числе похожих объектов.
Когда приспособленец не стоит использовать?
Когда объектов немного и память не критична, когда общих свойств мало, либо когда накладные расходы на поиск и управление кэшем перевешивают выгоду. В JavaScript часто проще прототипное наследование.
Исходный текст
SKILL.md · MIT
---
name: flyweight-pattern
description: Teaches the flyweight pattern for memory optimization. Use when your application creates large numbers of similar objects and memory consumption is a concern.
paths:
- "**/*.js"
- "**/*.ts"
license: MIT
metadata:
author: patterns.dev
version: "1.1"
related_skills:
- "module-pattern"
- "singleton-pattern"
---
# Flyweight Pattern
The flyweight pattern is a useful way to conserve memory when we're creating a large number of similar objects.
In our application, we want users to be able to add books. All books have a `title`, an `author`, and an `isbn` number! However, a library usually doesn't have just one copy of a book: it usually has multiple copies of the same book.
## When to Use
- Use this when creating a huge number of objects that could potentially drain available memory
- This is helpful when many objects share the same intrinsic properties (e.g., books with the same ISBN)
## When NOT to Use
- When the number of objects is small enough that memory is not a concern
- When objects have few or no shared intrinsic properties — the separation of intrinsic and extrinsic state adds complexity without savings
- When the added lookup/management overhead outweighs the memory benefit
## Instructions
- Separate intrinsic (shared) state from extrinsic (unique) state
- Use a Map or similar structure to cache and reuse shared object instances
- Consider JavaScript's prototypal inheritance as a simpler alternative in many cases
## Details
It wouldn't be very useful to create a new book instance each time if there are multiple copies of the exact same book. Instead, we want to create multiple instances of the `Book` constructor, that represent a single book.
```js
class Book {
constructor(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
}
```
Let's create the functionality to add new books to the list. If a book has the same ISBN number, thus is the exact same book type, we don't want to create an entirely new `Book` instance. Instead, we should first check whether this book already exists.
```js
const books = new Map();
const createBook = (title, author, isbn) => {
const existingBook = books.has(isbn);
if (existingBook) {
return books.get(isbn);
}
};
```
If it doesn't contain the book's ISBN number yet, we'll create a new book and add its ISBN number to the `isbnNumbers` set.
```js
const createBook = (title, author, isbn) => {
const existingBook = books.has(isbn);
if (existingBook) {
return books.get(isbn);
}
const book = new Book(title, author, isbn);
books.set(isbn, book);
return book;
};
```
The `createBook` function helps us create new instances of one type of book. However, a library usually contains multiple copies of the same book! Let's create an `addBook` function, which allows us to add multiple copies of the same book. It should invoke the `createBook` function, which returns either a newly created `Book` instance, or returns the already existing instance.
In order to keep track of the total amount of copies, let's create a `bookList` array that contains the total amount of books in the library.
```js
const bookList = [];
const addBook = (title, author, isbn, availability, sales) => {
const book = {
...createBook(title, author, isbn),
sales,
availability,
isbn,
};
bookList.push(book);
return book;
};
```
Perfect! Instead of creating a new `Book` instance each time we add a copy, we can effectively use the already existing `Book` instance for that particular copy. Let's create 5 copies of 3 books: Harry Potter, To Kill a Mockingbird, and The Great Gatsby.
```js
addBook("Harry Potter", "JK Rowling", "AB123", false, 100);
addBook("Harry Potter", "JK Rowling", "AB123", true, 50);
addBook("To Kill a Mockingbird", "Harper Lee", "CD345", true, 10);
addBook("To Kill a Mockingbird", "Harper Lee", "CD345", false, 20);
addBook("The Great Gatsby", "F. Scott Fitzgerald", "EF567", false, 20);
```
Although there are 5 copies, we only have 3 `Book` instances!
The flyweight pattern is useful when you're creating a huge number of objects, which could potentially drain all available RAM. It allows us to minimize the amount of consumed memory.
In JavaScript, we can easily solve this problem through [prototypal inheritance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain). Nowadays, hardware has GBs of RAM, which makes the flyweight pattern less important.
## Source
- [patterns.dev/vanilla/flyweight-pattern](https://patterns.dev/vanilla/flyweight-pattern)
### References
- [Flyweight](https://refactoring.guru/design-patterns/flyweight) - Refactoring Guru
- [Flyweight Design Pattern](https://howtodoinjava.com/design-patterns/structural/flyweight-design-pattern) - How To Do In Java
© patterns.dev · MIT · PatternsDev/skills