JS设计模式之享元模式

享元模式解决创建大量类似对象而导致的性能问题。

示例:

一个汽车类

//汽车登记示例
   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类代码

//包含核心数据的Car类
   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){
                 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
				 }         
       }
     }
   })();
管理外在装态
//数据工厂,用来处理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];
       }
     }
   })();
通过carRecordDatabase这个对象将内在对象与外在对象整合起来,享元对象与其所属的方法集中管理。相当于是哪一类型的车是一个对象,然后这个对象与其余的外在对象关联,减少对象的声明。

Web日历

不用享元模式,用组合模式包装,然后要创建365个CalendarDay对象,一年的话

var CalendarYear = function(year, parent){
		this.year = year;
		this.element = document.createElement('div');
		this.element.style.display = 'none';
		parent.appendChild(this.element);
		function isLeapYear(y){
		    return (y>0)&&!(y%4)&&((y%100)||!(y%400));
		}
		
		this.months = [];
		this.numDays = [31,isisLeapYear(this.year)?29:28,31,30,31,30,31,31,30,31,30,31];
		for(var i = 0,len = 12; i<len; i++){
		    this.months[i] = new CalendarMonth(i, this.numDays[i], this.element);
		}
	};
	
	CalendarYear.prototype = {
	    display:function(){
		    for(var i = 0, len = this.months.length; i<len; i++){
			    this.months[i].display();
			}
			this.element.style.dispaly = 'block';
		}
	};
	
	var CalendarMonth = function(monthNum, numDays, parent){
	    this.monthNum = monthNum;
		this.element = document.createElement('div');
		this.element.style.display = 'none';
		parent.appendChild(this.element);
		
		this.days = [];
		for(var i = 0, len = this.days.length; i < len; i++){
		    this.days[i] = new CalendarDay(i, this.element);
		}
	};
	
	CalendarMonth.prototype = {
	    display:function(){
		    for(var i = 0, len = this.days.length; i<len; i++){
			    this.days[i].display();
			}
			this.element.style.dispaly = 'block';
		}
	};
	
	var CalendarDay = function(date, parent){
	    this.date = date;
		this.element = document.createElement('div');
		this.element.style.display = 'none';
		parent.appendChild(this.element);
	};
	
	CalendarDay.prototype = {
	    display: function(){
		    this.element.style.display = 'block';
			this.element.innerHTML = this.date;
		}
	};

用享元模式

var CalendarDay = function(){};
	
	CalendarDay.prototype = {
	    display: function(date, parent){
		    var element = document.createElement('div');
			parent.appendChild(element);
			element.innerHTML = date;
		}
	};
	
	var calendarDay = new CalendarDay();
创建单个实例,以前的构造参数成为display的方法参数,然后修改CalendayMonth

var CalendarMonth = function(monthNum, numDays, parent){
	    this.monthNum = monthNum;
		this.element = document.createElement('div');
		this.element.style.display = 'none';
		parent.appendChild(this.element);
		
		this.days = [];
		for(var i = 0, len = this.days.length; i < len; i++){
		    this.days[i] = calendarDay;
		}
	};
	
	CalendarMonth.prototype = {
	    display:function(){
		    for(var i = 0, len = this.days.length; i<len; i++){
			    this.days[i].display(i, this.element);
			}
			this.element.style.dispaly = 'block';
		}
	};

将公用一个类,只是类的方法参数不同。这样内在数据只是calendarDay这个对象,外在对象日期等保存在组合对象的结构本身。

这样组合对象的叶对象只包含少量数据,就抽离出来,转化为共享资源。


var Tooltip = function(targetElement, text){
	    this.target = targetElement;
		this.text = text;
		this.delayTimeout = null;
		this.delay = 1500;
		
		this.element = document.createElement('div');
		this.element.style.display = 'none';
		this.element.style.position = 'absolute';
		this.element.className = 'tooltip';
		document.getElementsByTagName('body')[0].appendChild(this.element);
		this.element.innerHTML = this.text;
		
		var that = this;
		addEvent(this.target, 'mouseover', function(e){that.startDelay(e);});
		addEvent(this.target, 'mouseout', function(e){that.hide();});
	};
	
	Tooltip.prototype = {
	    startDelay:function(e){
		    if(this.delayTimeout == null){
			    var that = this;
				var x = e.clientX;
				var y = e.clientY;
				this.delayTimeout = setTimeout(function(){
				    this.show(x,y);
				},this.delay);
			}
		},
		show:function(x, y){
		    clearTimeout(this.delayTimeout);
			this.delayTimeout = null;
			this.element.style.left = x + 'px';
			this.element.style.top = y + 'px';
			this.element.style.display = 'block';
		},
		hide:function(){
		    clearTimeout(this.delayTimeout);
			this.delayTimeout = null;
			this.element.style.display = 'none';
		}
	};

使用享元模式

var Tooltip = function(){
		this.delayTimeout = null;
		this.delay = 1500;
		
		this.element = document.createElement('div');
		this.element.style.display = 'none';
		this.element.style.position = 'absolute';
		this.element.className = 'tooltip';
		document.getElementsByTagName('body')[0].appendChild(this.element);
	};
	
	Tooltip.prototype = {
	    startDelay:function(e, text){
		    if(this.delayTimeout == null){
			    var that = this;
				var x = e.clientX;
				var y = e.clientY;
				this.delayTimeout = setTimeout(function(){
				    this.show(x, y, text);
				},this.delay);
			}
		},
		show:function(x, y, text){
		    clearTimeout(this.delayTimeout);
			this.delayTimeout = null;
			this.element.innerHTML = text;
			this.element.style.left = x + 'px';
			this.element.style.top = y + 'px';
			this.element.style.display = 'block';
		},
		hide:function(){
		    clearTimeout(this.delayTimeout);
			this.delayTimeout = null;
			this.element.style.display = 'none';
		}
	};

享元对象,将外在数据先全部剔除。

var TooltipManager = (function(){
	    var storeInstance = null;
		var Tooltip = function(){
			this.delayTimeout = null;
			this.delay = 1500;
			
			this.element = document.createElement('div');
			this.element.style.display = 'none';
			this.element.style.position = 'absolute';
			this.element.className = 'tooltip';
			document.getElementsByTagName('body')[0].appendChild(this.element);
		};
		
		Tooltip.prototype = {
			startDelay:function(e, text){
				if(this.delayTimeout == null){
					var that = this;
					var x = e.clientX;
					var y = e.clientY;
					this.delayTimeout = setTimeout(function(){
						this.show(x, y, text);
					},this.delay);
				}
			},
			show:function(x, y, text){
				clearTimeout(this.delayTimeout);
				this.delayTimeout = null;
				this.element.innerHTML = text;
				this.element.style.left = x + 'px';
				this.element.style.top = y + 'px';
				this.element.style.display = 'block';
			},
			hide:function(){
				clearTimeout(this.delayTimeout);
				this.delayTimeout = null;
				this.element.style.display = 'none';
			}
	    };
		
		return  {
		    addTooltip:function(targetElement, text){
			    var tt = this.getTooltip();
				addEvent(targetElement, 'mouseover', function(e){that.startDelay(e);});
		        addEvent(targetElement, 'mouseout', function(e){that.hide();});
			},
			getTooltip:function(){
			    if(storeInstance == null){
				    storeInstance = new Tooltip();
					return storeInstance;
				}
			}
		};
	})();

外在对象被闭包保存。


保存实例供以后重用

var DialogBox = function(){};
	DialogBox.prototype = {
	    show:function(header, body, footer){},
	    hide:function(){},
		state:function(){}
	};
	DialogBoxManager = (function(){
	    var created = [];
		return {
		    displayDialogBox:function(header, body, footer){
			    var inUse = this.numberInUse();
				if(inUse > created.length){
				    created.push(this.createDialogBox());
				}
				created[inUse].show(header, body, footer);
			},
			createDialogBox:function(){
			    var db = new DialogBox();
				return db;
			},
			numberInUse:function(){
			    var inUse = 0;
				for(var i = 0, len = created.length; i < len; i++){
				    if(created[i].state = 'visible'){
					    inUse++;
					}
				}
				return inUse;
			}
		};
	})();

这里将创建出的DialogBox保存在created这个闭包变量中,然后获取正在被使用的个数,如果都被使用,就创建,否则就重用。

这样内在对象就就一直被重用,然后外在对象保存在闭包变量中,这里复杂了一下,就相当与数据库的连接池。

享元模式使用的场合:

第一:网页中使用了大规模的密集对象。

第二:这些对象的内部数据能够分离出来,作为参数提供给方法。

第三:分离出的内在的对象比较少,尽量减少独一无二的对象。

享元模式的一般步骤:

1、将所有外在数据从目标类中剥离。尽量删除该类的属性跟构造函数,然后提供给该类的方法。结果是功能一样,数据的来源与方法的参数。

2、创建一个控制该类的实例化工厂,保存创造出的独一无二的对象。一种方法是用一个对象保存引用,然后用参数的唯一指定索引,另一种是对象池,用数组保存引用。

3、创建保存外在数据的管理器。把内在数据提供给工厂对象以创造对象,外在数据保存在管理器的数据结构中。

享元模式之利:

降低网页负荷。

享元模式之弊:

只是在一系列条件下满足的优化,然后创造的对象变少,追踪数据问题困难,维护困难。

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值