11.Javascript设计模式之享元模式----Flyweight

11.Javascript设计模式之享元模式----Flyweight

GOF:运用共享技术有效地支持大量细粒度的对象。

享元模式概念解释

享元模式:也就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。 比如说(这里引用GOF书中的例子)一个文本系统,每个字母定一个对象,那么大小写字母一共就是52个,那么就要定义52个对象。 如果有一个1M的文本,那么字母是何其的多,如果每个字母都定义一个对象那么内存早就爆了。 那么如果要是每个字母都共享一个对象,那么就大大节约了资源。

在Flyweight模式中,由于要产生各种各样的对象,所以在Flyweight(享元)模式中常出现Factory模式。 Flyweight的内部状态是用来共享的,Flyweight factory负责维护一个对象存储池(Flyweight Pool)来存放内部状态的对象。 Flyweight模式是一个提高程序效率和性能的模式,会大大加快程序的运行速度.应用场合很多。

享元模式示例

下面列举一个小汽车注册的例子。

非最优化的设计方式
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);
    }
};

在短时间内,这个注册系统能够进行正常工作,但是,当你所在的城市的人群逐渐增多,你会意识到这个系统会运行得越来越慢。 成千上万的小汽车对象已经溢出了系统的可用资源,为了优化这个系统,使其长期稳定运行,我们需要借助享元模式。

使用享元模式
第一.定义Car类
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;
    }
};
第二.定义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;
            }
        }
    };
})();
第三.定义一个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);
        }
    };
})();

这个估计得花点儿时间才能理解了,So do I...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值