上次我们简单介绍了静态成员,今天再延伸一下,看看设计模式中的单件模式(Singleton),我先照搬一些理论 Singleton-对象创建型模式
可是实际中如何运用呢?据个例子,我们现在要做一个游戏,而游戏中需要加载一些Sprite,这些Sprite来自于一些bmp资源,为了提升性能并使程序中只有一个实例,这时我们考虑使用Singleton模式
方法的调用 小节:通过Singleton,我们可以方便的使用类的实例方法,同时保证类的实例的唯一性。<script language="javascript" type="text/javascript"> var pb_strConfirmCloseMessage; var pb_blnCloseWindow = false; pb_strConfirmCloseMessage ="您在查看Truly的Blog,真的要离开本页吗?"; function ConfirmClose() { window.event.returnValue = pb_strConfirmCloseMessage; pb_blnCloseWindow = true; } function ShowConfirmClose(blnValue) { if(blnValue) { document.body.onbeforeunload = ConfirmClose; } else { document.body.onbeforeunload = null; } }ShowConfirmClose(true); function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1;eval(i + decodeURI("%3c") + numTabs); i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; eval(j + decodeURI("%3c") + numTabs); j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } </script> |
Singleton深入浅出
最新推荐文章于 2024-09-22 18:54:04 发布