Home > Web Front-end > JS Tutorial > js design pattern: what is flyweight pattern? Introduction to js flyweight mode

js design pattern: what is flyweight pattern? Introduction to js flyweight mode

不言
Release: 2018-08-17 16:39:26
Original
1401 people have browsed it

This article brings you content about js design patterns: What is the flyweight pattern? The introduction of js flyweight mode has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is flyweight mode?

Definition: Flyweight mode is a mode that optimizes program performance. Its essence is to reduce the number of objects created.

Main solution: When there are a large number of objects, it may cause memory overflow. We abstract the common parts. If there is the same business request, directly return the data already stored in the memory. Some objects are avoided from being recreated.

When to use: 1. There are a large number of objects in the system. 2. These objects consume a lot of memory. 3. Most of the status of these objects can be externalized. 4. These objects can be divided into many groups according to their intrinsic states. When extrinsic objects are removed from the objects, each group of objects can be replaced by an object. 5. The system does not rely on the identity of these objects, and these objects are indistinguishable.

How to solve: Use the unique identification code to judge. If there is one in the memory, return the object identified by the unique identification code.

Key code: Use hash objects to store these objects.

js flyweight mode application example: 1. For example, string, if it exists, return it, if not, create a string and save it in the string cache pool. 2. The data pool of the database.

Advantages of js flyweight mode: Greatly reduce the creation of objects, reduce system memory, and improve efficiency.

Disadvantages of js flyweight mode: Increases the complexity of the system, requires separation of external state and internal state, and the external state has an inherent nature and should not change with the internal state Change according to changes, otherwise it will cause chaos in the system.

js flyweight mode usage scenarios: 1. The system has a large number of similar objects. 2. Scenarios that require a buffer pool.

js Flying Dollar Model Example:

A certain merchant has 50 kinds of men’s underwear and 50 kinds of women’s underwear, and wants to display them

Option 1: Make 50 plastics Let them wear male models and 50 plastic female models for display. The code is as follows:

const Model = function(gender, underwear) {
  this.gender = gender
  this.underwear = underwear
}

Model.prototype.takephoto = function() {
  console.log(`${this.gender}穿着${this.underwear}`)
}

for (let i = 1; i < 51; i++) {
  const maleModel = new Model(&#39;male&#39;, `第${i}款衣服`)
  maleModel.takephoto()
}

for (let i = 1; i < 51; i++) {
  const female = new Model(&#39;female&#39;, `第${i}款衣服`)
  female.takephoto()
}
Copy after login

Option 2: Make 1 plastic male model and 1 plastic female model, and try on 50 styles of underwear respectively

const Model = function(gender) {
    this.gender = gender
}

Model.prototype.takephoto = function() {
    console.log(`${this.gender}穿着${this.underwear}`)
}

const maleModel = new Model(&#39;male&#39;)
const femaleModel = new Model(&#39;female&#39;)

for (let i = 1; i < 51; i++) {
    maleModel.underwear = `第${i}款衣服`
    maleModel.takephoto()
}

for (let i = 1; i < 51; i++) {
    femaleModel.underwear = `第${i}款衣服`
    femaleModel.takephoto()
}
Copy after login

Comparison found: Plan 1 created 100 objects, Plan 2 only created 2 objects. In this demo, gender is an internal object and underwear is an external object.

Of course, in the demo of option 2, further improvements can be made:

  1. Create the instance explicitly through the constructor from the beginning, and use factory mode to upgrade it to a reusable instance. Controlled generation

  2. It is not very elegant to manually add underwear on the instance. You can write a separate manager function externally

const Model = function(gender) {
  this.gender = gender
}

Model.prototype.takephoto = function() {
  console.log(`${this.gender}穿着${this.underwear}`)
}

const modelFactory = (function() { // 优化第一点
  const modelGender = {}
  return {
    createModel: function(gender) {
      if (modelGender[gender]) {
        return modelGender[gender]
      }
      return modelGender[gender] = new Model(gender)
    }
  }
}())

const modelManager = (function() {
  const modelObj = {}
  return {
    add: function(gender, i) {
      modelObj[i] = {
        underwear: `第${i}款衣服`
      }
      return modelFactory.createModel(gender)
    },
    copy: function(model, i) { // 优化第二点
      model.underwear = modelObj[i].underwear
    }
  }
}())

for (let i = 1; i < 51; i++) {
  const maleModel = modelManager.add(&#39;male&#39;, i)
  modelManager.copy(maleModel, i)
  maleModel.takephoto()
}

for (let i = 1; i < 51; i++) {
  const femaleModel = modelManager.add(&#39;female&#39;, i)
  modelManager.copy(femaleModel, i)
  femaleModel.takephoto()
}
Copy after login

Related recommendations:

js design pattern: What is the combination pattern? Introduction to js combination pattern

#js design pattern: What is the template method pattern? Introduction to js template method pattern

The above is the detailed content of js design pattern: what is flyweight pattern? Introduction to js flyweight mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template