zoukankan      html  css  js  c++  java
  • javascript 享元模式 flyweight

    * 适应条件

    ** 一个程序中使用了大量的相似对象 造成大的内存开销

    ** 对象的大多数状态都可以变为外部状态

    ** 剥离出对象的外部状态之后, 可以使用相对较少的共享对象取代大量对象

    * 上传文件的例子:

    index.html

    <html>
    <head>
    	<meta charset="UTF-8">
    	<title>一个上传多个文件的例子</title>
    </head>
    <body>
    <script src="js/upload.js"></script>
    </body>
    </html>
    

      

    js/upload.js

    function Upload(uploadType) {
    	this.uploadTpye = uploadType;
    }
    
    var UploadFactory = (function() {
    	var c = {};  // cache
    
    	return {
    		// @param t: uploadType
    		create: function(t) {
    			if (c[ t ]) {
    				return c[ t ];
    			}
    			return c[ t ] = new Upload( t );
    		}
    	}
    })();
    
    var uploadManager = (function() {
    	// uploadDatabase
    	var db = {}, autoInc = 1;	
    
    	return {
    		add: function(uploadType, file) {
    			var o = UploadFactory.create(uploadType),
    				id = autoInc++;
    
    			var dom = document.createElement('div');
    			dom.setAttribute("data-id", id);  // debug
    			dom.innerHTML =   
    "<span>文件名称: " + file.fileName + ", 文件大小: " + file.fileSize + "</span>" +
    "<button class='delFile'>删除</button>";
    			dom.querySelector(".delFile").onclick = function() {
    				o.delFile(id);
    			};
    			document.body.appendChild(dom);
    
    			db[ id ] = {
    				fileName: file.fileName,
    				fileSize: file.fileSize,
    				dom: dom
    			};
    			return o;
    		},
    		setExternalState: function(id, o) {
    			var uploadData = db[id];
    			Object.keys(uploadData).forEach(function(prop) {
    				o[ prop ] = uploadData[ prop ];
    			});
    		}
    	}
    })();
    
    Upload.prototype.delFile = function(id) {
    	uploadManager.setExternalState(id, this);
    	
        if (this.fileSize < 3000) {
            return this.dom.parentNode.removeChild(this.dom);
        }
    
        if (window.confirm('确定要删除该文件吗?' + this.fileName)) {
            return this.dom.parentNode.removeChild(this.dom);
        }
    }
    
    window.startUpload = function(uploadType, files) {
    	files.forEach(function(file) {
    		var uploadObj = uploadManager.add(uploadType, file);
    	});
    };
    
    startUpload('plugin', [
        { fileName: '1.txt', fileSize: 1000 },
        { fileName: '2.html', fileSize: 3000 },
        { fileName: '3.txt', fileSize: 5000 }
    ]);
    
    startUpload('flash', [
        { fileName: '1.txt', fileSize: 1000 },
        { fileName: '2.html', fileSize: 3000 },
        { fileName: '3.txt', fileSize: 5000 }
    ]);
    

      

  • 相关阅读:
    [LeetCode] Power of Three 判断3的次方数
    [LeetCode] 322. Coin Change 硬币找零
    [LeetCode] 321. Create Maximum Number 创建最大数
    ITK 3.20.1 VS2010 Configuration 配置
    VTK 5.10.1 VS2010 Configuration 配置
    FLTK 1.3.3 MinGW 4.9.1 Configuration 配置
    FLTK 1.1.10 VS2010 Configuration 配置
    Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别
    [LeetCode] Bulb Switcher 灯泡开关
    [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
  • 原文地址:https://www.cnblogs.com/mingzhanghui/p/9396676.html
Copyright © 2011-2022 走看看