Home > Article > Web Front-end > js design pattern: what is flyweight pattern? Introduction to js flyweight mode
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.
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.
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('male', `第${i}款衣服`) maleModel.takephoto() } for (let i = 1; i < 51; i++) { const female = new Model('female', `第${i}款衣服`) female.takephoto() }
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('male') const femaleModel = new Model('female') for (let i = 1; i < 51; i++) { maleModel.underwear = `第${i}款衣服` maleModel.takephoto() } for (let i = 1; i < 51; i++) { femaleModel.underwear = `第${i}款衣服` femaleModel.takephoto() }
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:
Create the instance explicitly through the constructor from the beginning, and use factory mode to upgrade it to a reusable instance. Controlled generation
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('male', i) modelManager.copy(maleModel, i) maleModel.takephoto() } for (let i = 1; i < 51; i++) { const femaleModel = modelManager.add('female', i) modelManager.copy(femaleModel, i) femaleModel.takephoto() }
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!