“This is the 27th day of my participation in the August Gwen Challenge.

preface

At the advanced end of the interview, there are often some questions about design patterns, and the answers are not good. Coincides with the activities of more challenges in August, I plan to spend a month to clarify the knowledge points about design patterns, to increase the confidence of my interview.

What is the share mode

Sharing mode is a mode for performance optimization. The core of sharing mode is to support a large number of fine-grained objects effectively by using sharing technology.

The share pattern is useful if the system is overloaded with memory due to the creation of a large number of similar objects.

The share pattern requires that the attributes of an object be divided into internal and external attributes. The goal of the share pattern is to minimize the number of shared objects. Here are some suggestions for separating internal state from external state:

  • Internal properties are stored inside objects.
  • Internal properties can be shared by several objects.
  • Internal attributes are scenario-independent and generally do not change.
  • External attributes depend on and vary from scenario to scenario, and cannot be shared.

Specify all objects with the same internal properties as the same shared object. External attributes can be stripped from the object and stored externally. Objects stripped of external attributes become shared objects, and external attributes are passed into the shared object as necessary to assemble a complete object. While the process of assembling external attributes into a complete object takes time, it can significantly reduce the number of objects in the system, which may be trivial in comparison. Therefore, enjoy element mode is a time for space optimization mode.

Implement a share pattern

Imagine a clothing factory that currently produces 50 types of men’s coats and 50 types of women’s coats. In order to promote their products, the factory decides to produce plastic models to wear their coats for advertising photos. Normally you would have 50 male models and 50 female models, and then you would have each of them put on a separate coat for the photo shoot. Instead of using the share mode, an application might implement something like this:

class Model {
  constructor(sex, looseCoat) {
    this.sex = sex;
    this.looseCoat = looseCoat;
  }
  takePhoto() {
    console.log('sex= ' + this.sex + ' looseCoat=' + this.looseCoat);
  }
}
for (let i = 1; i <= 50; i++) {
  const maleModel = new Model('male', 'looseCoat' + i);
  maleModel.takePhoto();
};
for (let j = 1; j <= 50; j++) {
  const femaleModel = new Model('female', 'looseCoat' + j);
  femaleModel.takePhoto();
};
Copy the code

If you follow the procedure above, those 50 coats for men and 50 coats for women will produce 100 models. If you have 10,000 men’s coats and 10,000 women’s coats, you don’t need 10,000 models, and the program will probably collapse prematurely because there are so many objects. In reality, however, there are no such stupid factories. All it takes is one male model and one female model.

So the program should be optimized like this.

The Model only needs to accept sex parameters and only needs to distinguish between male and female models.

class Model { constructor(sex) { this.sex = sex; } takePhoto() { console.log('sex= ' + this.sex + ' looseCoat=' + this.looseCoat); }}Copy the code

Create a male model object and a female model object respectively:

const maleModel = new Model('male');
const femaleModel = new Model('female');
Copy the code

The male models were photographed wearing all the men’s clothes in turn:

for (let i = 1; i <= 50; i++) {
  maleModel.looseCoat = 'looseCoat' + i;
  maleModel.takePhoto();
};
Copy the code

Similarly, the female models were photographed wearing all the dresses in turn:

for (var j = 1; j <= 50; j++) {
  femaleModel.looseCoat = 'looseCoat' + j;
  femaleModel.takePhoto();
};
Copy the code

The above is a very classic yuan model.

Take the sex of the model as the internal attribute of the model, and the coat looseCoat on the model as the external attribute. After stripping the looseCoat attribute of the coat, the object of the model can be shared, and then attach the looseCoat attribute of the coat to the model when necessary to form a complete object.

When to use the premium mode

The benefits of the share pattern depend largely on how and when it is used, and in general, the share pattern can be used when:

  • A large number of similar objects are used in a program.
  • Because a large number of objects are used, there is a large memory overhead.
  • Most of the state of an object can be changed to an external state.
  • Once you strip away the external state of an object, you can replace a large number of objects with relatively few shared objects.