11. Enjoy the metadata mode in the Javascript design mode -- flyweight

Source: Internet
Author: User
11. Enjoy the metadata of the Javascript design model ---- flyweight

Gof: Use the sharing technology to effectively support a large number of fine-grained objects.

Concepts

Share mode: if multiple identical objects exist in a system, you can share only one object without instantiating one object.
For example, in a text system (Here we reference the example in the gof book), each letter is an object, so there are 52 uppercase and lowercase letters in total, so 52 objects must be defined.
If there is a 1 m text, then there are many letters. If each letter defines an object, the memory will have exploded.
Therefore, if each letter shares an object, the resource is greatly reduced.

In the flyweight mode, the factory mode is often used in the flyweight mode because various objects are generated.
The internal state of Flyweight is used for sharing. flyweight factory maintains an object storage pool to store objects in the internal state.
The flyweight mode is a mode for improving program efficiency and performance, which greatly speeds up program running. There are many application scenarios.

Example of the metadata Mode

The following is an example of car registration.

Non-Optimal Design Method
var Car = function(make, model, year, owner, tag, renewDate) {    this.make = make;    this.model = model;    this.year = year;    this.owner = owner;    this.tag = tag;    this.renewDate = renewDate;};Car.prototype = {    getMake: function() {        return this.make;    },    getModel: function() {        return this.model;    },    getYear: function() {        return this.year;    },    transferOwnership: function(newOwner, newTag, newRenewDate) {        this.owner = newOwner;        this.tag = newTag;        this.renewDate = newRenewDate;    },    renewRegistration: function(newRenewDate) {        this.renewDate = newRenewDate;    },    isRegistrationCurrent: function() {        var today = new Date();        return today.getTime() < Date.parse(this.renewDate);    }};

In a short period of time, this registration system can work normally. However, when the number of people in your city increases, you will realize that the system will run slowly.
Thousands of car objects have exceeded the available resources of the system. To optimize the system and ensure its long-term stable operation, we need to use the enjoy model.

Use the shared element mode. 1. Define the car class.
var Car = function(make, model, year) {    this.make = make;    this.model = model;    this.year = year;};Car.prototype = {    getMake: function() {        return this.make;    },    getModel: function() {        return this.model;    },    getYear: function() {        return this.year;    }};
2. Define carfactory
/* CarFactory singleton. */var CarFactory = (function() {    var createdCars = {};    return {        createCar: function(make, model, year) {            // Check to see if this particular combination has been created before.            if(createdCars[make + '-' + model + '-' + year]) {                return createdCars[make + '-' + model + '-' + year];            }            // Otherwise create a new instance and save it.            else {                var car = new Car(make, model, year);                createdCars[make + '-' + model + '-' + year] = car;                return car;            }        }    };})();
3. Define a carrecordmanager
/* CarRecordManager singleton. */var CarRecordManager = (function() {    var carRecordDatabase = {};        return {        // Add a new car record into the city's system.        addCarRecord: function(make, model, year, owner, tag, renewDate) {            var car = CarFactory.createCar(make, model, year);            carRecordDatabase[tag] = {                owner: owner,                renewDate: renewDate,                car: car            };        },        // Methods previously contained in the Car class.        transferOwnership: function(tag, newOwner, newTag, newRenewDate) {            var record = carRecordDatabase[tag];            record.owner = newOwner;            record.tag = newTag;            record.renewDate = newRenewDate;        },        renewRegistration: function(tag, newRenewDate) {            carRecordDatabase[tag].renewDate = newRenewDate;        },        isRegistrationCurrent: function(tag) {            var today = new Date();            return today.getTime() < Date.parse(carRecordDatabase[tag].renewDate);        }    };})();

This estimation takes some time to understand, so do I...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.