zoukankan      html  css  js  c++  java
  • javascript设计模式-享元模式

    享元模式采用一个共享来有效的支持大量细小粒度的对象,避免大量有相同内容的类的开销(如内存耗费),共享一个元类。

    应用场景:页面存在大量的资源密集型对象;他们具备一些共性,可以分离出公共操作的数据。

    一个例子,汽车登记:

        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(){
           returnthis.make;
         },
         getModel:function(){
           returnthis.model;
         },
         getYear:function(){
           returnthis.year;
         },
         transferOwner:function(owner,tag,renewDate){
           this.owner=owner;
           this.tag=tag;
           this.renewDate=renewDate;
         },
         renewRegistration:function(renewDate){
           this.renewDate=renewDate;
         }
        };  

    如果汽车数量多了,就使用享元模式。可以将数据分成内部和外部两种数据,和car对象相关的数据(make, model等)可以归结为内部属性,而(owner, renewDate等)可以归结为外部属性。这样,如下代码就可以在同一车里共享同一个对象了,不管转让给谁,只要车是同一车,基本信息是一样的:

    var Car=function(make,model,year){
         this.make=make;
         this.model=model;
         this.year=year;
    };
    Car.prototype={
         getMake:function(){
           returnthis.make;
         },
         getModel:function(){
           returnthis.model;
         },
         getYear:function(){
           returnthis.year;
         }
    };
    //中间对象,用来实例化Car类
    var CarFactory=(function(){
           var createdCars = {};
           return {
           createCar:function(make,model,year){
             var car=createdCars[make+"-"+model+"-"+year];
             return car ? car : createdCars[make +'-'+ model +'-'+ year] =(new Car(make,model,year));
           }
         };
    })();
    //数据工厂,用来处理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,
               tag:tag,
               renewDate:renewDate,
               car:car
           };
         },
         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;
         },
         getCarInfo:function(tag){
             return carRecordDatabase[tag];
         }
       };
    })();

    通过这种方式,我们做到了将同一车的相同信息保存在一个CarRecordManager对象里,而且只保存一份;相比之前的代码,就可以发现节约了很多内存。

    如果一个应用程序使用了大量的对象,而这些大量的对象造成了很大的存储开销时就应该考虑使用享元模式。

  • 相关阅读:
    STL源码分析:Algorithms
    STL源码分析:Functors
    STL源码分析:Adapters
    Frost R&D
    Black World
    Houdini Krakatoa Render Plugin
    C++ Template 编程,泛型编程练习
    LeetCode中涉及到的数据结构和算法的编程题总结
    细说线程池
    细说线程安全
  • 原文地址:https://www.cnblogs.com/linda586586/p/4251089.html
Copyright © 2011-2022 走看看