flyweight-pattern

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Flyweight 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.
享元模式是一种在创建大量相似对象时节省内存的实用方法。
在我们的应用中,我们希望用户能够添加图书。所有图书都有
title
author
isbn
编号!不过,图书馆通常不会只有某本书的一本副本:通常同一本书会有多个副本。

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)
  • 当需要创建的对象数量极多,可能耗尽可用内存时使用
  • 当多个对象具有相同的内在属性时(例如ISBN相同的图书),该模式非常有用

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
  • 将内在(共享)状态与外在(唯一)状态分离
  • 使用Map或类似结构缓存并复用共享的对象实例
  • 多数场景下可以考虑使用JavaScript的原型继承作为更简单的替代方案

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.
如果完全相同的图书有多个副本,每次都创建新的Book实例性价比很低。我们可以只创建单个
Book
实例来代表同一种图书。
js
class Book {
  constructor(title, author, isbn) {
    this.title = title;
    this.author = author;
    this.isbn = isbn;
  }
}
下面我们来实现向列表中添加新图书的功能。如果两本书的ISBN编号相同,说明它们属于同一种图书,我们不需要创建全新的
Book
实例,而是先检查这本书是否已经存在。
js
const books = new Map();

const createBook = (title, author, isbn) => {
  const existingBook = books.has(isbn);

  if (existingBook) {
    return books.get(isbn);
  }
};
如果ISBN编号对应的图书还不存在,我们就创建一本新的图书,并将它的ISBN编号添加到
books
Map中。
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;
};
createBook
函数帮我们为每种图书创建唯一的实例。不过,图书馆通常会有同一本书的多个副本!我们来创建一个
addBook
函数,支持添加同一本书的多个副本。它会调用
createBook
函数,要么返回新创建的
Book
实例,要么返回已经存在的实例。
为了统计副本的总数量,我们创建一个
bookList
数组来存储图书馆中的所有图书副本。
js
const bookList = [];

const addBook = (title, author, isbn, availability, sales) => {
  const book = {
    ...createBook(title, author, isbn),
    sales,
    availability,
    isbn,
  };

  bookList.push(book);
  return book;
};
完美!现在我们添加副本时不用每次都创建新的
Book
实例,而是可以直接复用对应图书已有的
Book
实例。我们来为3本书创建5个副本:《哈利·波特》、《杀死一只知更鸟》和《了不起的盖茨比》。
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);
虽然有5个副本,我们却只创建了3个
Book
实例!
享元模式在你需要创建极多对象时非常有用,这些对象可能会耗尽所有可用RAM,它可以帮我们最大程度减少内存消耗。
在JavaScript中,我们可以通过原型继承轻松解决这个问题。如今硬件的RAM已经达到了GB级别,享元模式的重要性有所降低。

Source

来源

References

参考资料