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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值