Flyweight Pattern

patternsdev/skills/javascript/flyweight-pattern

作者 patternsdev48bf58a488cdMIT253 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5个月前更新

Teaches the flyweight pattern for memory optimization. Use when your application creates large numbers of similar objects and memory consumption is a concern.

AI 生成的概览

讲解享元模式,用于在创建大量相似对象时降低内存占用。

功能
该技能讲解享元设计模式及其适用时机。它说明如何区分内在共享状态与外在独有状态、如何用 Map 缓存并复用共享实例,并给出以 Book 对象和图书馆副本为例的 JavaScript 示例。它还指出不宜使用该模式的情形,并提到原型继承可作为更简单的替代方案。
适用场景
当应用需要创建数量庞大且可能耗尽内存的相似对象时使用。当许多对象共享相同的内在属性时也适用。对象数量较少或几乎没有共享状态时不适合使用。
运行要求
无需脚本或特殊工具,仅为说明性内容。示例使用 JavaScript,因此熟悉 JavaScript 类与 Map 会有帮助。

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. Nowadays, hardware has GBs of RAM, which makes the flyweight pattern less important.

Source

References

来源与署名

来源:patternsdev/skills位于javascript/flyweight-pattern提交48bf58a

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架