美文网首页
享元模式

享元模式

作者: niumew | 来源:发表于2016-04-14 11:01 被阅读0次

    运用共享技术有效的支持大量的细粒度的对象,避免对象间拥有相同内容造成多余的开销

    // 享元对象
    var Flyweight = function () {    
      var created = [];    
      function create() {        
      var dom = document.createElement('div');          
      document.getElementById('container').appendChild(dom);        
      created.push(dom);        
      return dom;    
    }    
    return {        
      getDiv : function() {            
        if (created.length < 5) {                
            return create();            
          } else {                
            var div = created.shift();                
            created.push(div);                
            return div;            
          }        
        }    
      }
    }();
    // 实现需求
    var paper = 0,    
          num = 5,    
          len = article.length;
    for (var i=0; i<5; i++) {    
        if (article[i]) {        
           Flyweight.getDiv().innerHTML = article[i];    
        }
    }
    document.getElementById('next_page').onclick = function () {    
      if (article.length < 5) {        
        return    
      }    
      var n = ++paper  * num % len,        
            j = 0;    
      for (; j < 5; j++) {        
        if (article[n + j]) {            
          Flyweight.getDiv().innerHTML = article[n + j];        
        } else if (article[n + j - len]) {            
          Flyweight.getDiv().innerHTML = article[n + j - len];        
        } else {            
          Flyweight.getDiv().innerHTML = "";        
        }    
      }
    }
    

    相关文章

      网友评论

          本文标题:享元模式

          本文链接:https://www.haomeiwen.com/subject/odcglttx.html