javascript设计模式-享元模式(10)

享元模式是一种优化模式,它最适合解决因创建类似对象而涉及性能的问题。这种模式在js中尤其有用,因为复杂的js代码可能很快就会用光浏览器的所有可用内存。通过把大量独立对象转化为少量共享对象,可降低运行web应用程序所需的资源数量

传统的多实例对象场景

//出厂商、型号、出厂日期、拥有者、车牌号、最近登记日期
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 = {
    constructor:Car  , 
    getMake :function(){
        return this.make;
    },
    getModel:function(){
        return this.model;
    },
    getYear:function(){
        return this.year;
    },
    renewRegistration:function(newRenewDate){
        this.renewDate = newRenewDate;
    }
};

var arr = [] ;

var stime = new Date().getTime();
for(var i = 0 ; i < 5000000; i ++){
    // runtime: 734ms  web: 570
    arr.push(new Car('上海大众','迈腾','2012-02-03','bhx','bj0011','2013-04-01'));
}
var etime = new Date().getTime();
alert(etime - stime);

享元模式的改进

//享元模式:优化的设计模式 (优化:时间[代码的运行时间]、空间[web浏览器内存])
    //享元模式:内在数据static (出厂商、型号、出厂日期)  外在数据(拥有者、车牌号、最近登记日期)


    //出厂商、型号、出厂日期、拥有者、车牌号、最近登记日期
    var Car = function(make, model, year) {
        this.make = make;
        this.model = model;
        this.year = year;
    };
    Car.prototype = {
        constructor: Car,
        getMake: function() {
            return this.make;
        },
        getModel: function() {
            return this.model;
        },
        getYear: function() {
            return this.year;
        }
    };

    //工厂模式(闭包工厂)
    var CarFactory = (function() {
        //用于承装已经生产好的car 
        var createdCars = {};
        return {
            createCar: function(make, model, year) {
                //如果createdCars对象里已经存在当前的make ,model , year
                if (createdCars[make + model + year]) {
                    return createdCars[make + model + year];
                } else {
                    var car = new Car(make, model, year);
                    createdCars[make + model + year] = car;
                    return car;
                }
            }
        };
    })();

    //单体模式(外在的数据 和内在的数据 结合在一起)
    var CarRecordManager = (function() {
        //把登记好的汽车放到这个对象里
        var carRecordDataBase = {};
        return {
            addCarRecord: function(make, model, year, owner, tag, renewDate) {
                var car = CarFactory.createCar(make, model, year);
                carRecordDataBase[tag] = {
                    owner: owner,
                    renewDate: renewDate,
                    car: car
                };
            },
            renewRegistration: function(tag, newRenewDate) {
                carRecordDataBase[tag].renewDate = newRenewDate;
            }
        };
    })();

    var arr = [];

    var stime = new Date().getTime();
    for (var i = 0; i < 5000000; i++) {
        // runtime: 734ms  web: 570
        // arr.push(new Car('上海大众','迈腾','2012-02-03','bhx','bj0011','2013-04-01'));
        //享元模式的测试
        // runtime: 300ms   web: 230
        arr.push(CarRecordManager.addCarRecord('上海大众', '迈腾', '2012-02-03', 'bhx', 'bj0011', '2013-04-01'));
    }
    var etime = new Date().getTime();
    alert(etime - stime);

你可能感兴趣的:(javaScript)