本文实例讲述了原生javascript单例模式的应用。分享给大家供大家参考,具体如下:
总体原则:开闭原则(Open Close Principle) 开闭原则就是说对扩展开放,对修改关闭。在程序需要进行扩展的时候,不能去修改原有 的代码,实现一个热插拔的效果。所以一句话概括就是:为了使程序的扩展性好,易于维护和升 级。 1、单一职责原则 不要存在多于一个导致类变更的原因,也就是说每个类应该实现单一的职责,如若不然,就 应该把类拆分。
创建模式_单例模式 某个类只允许创建一个实例,这就是单例模式。优点如下: 1、某些类创建比较频繁,对于一些大型的对象,这是一笔很大的系统开销 2、省去了new操作符,降低了系统内存的使用频率,减轻GC压力。 3、有些类如交易所的核心交易引擎,控制着交易流程,如果该类可以创建 多个的话,系统完全乱了。(比如:中国国家主席只有一个,飞机大战的 地图对象只有一个),所以只有使用单例模式,才能保证核心交易服务器 独立控制整个流程。
应用:
飞机大战中的地图只能有一个实例; 遮罩层有可能是某个项目中经常要频繁创建的实例,如果不停地创建和删除,还是 很浪费资源。应该,在首次使用时创建,以后只是使用首次创建的实例 放大镜有可能在某个项目中经常要频繁地创建的实例,也是很浪费资源。
单例模式的基础应用
飞机大战中单例模式的应用
地图部分
let mapSingleton = (function(){
function Map(width,height,background){
this.domObj = null;//地图的div
this.moveBox = null;
this.width = width;
this.height = height;
this.background = background;
this.enemyPlanes = [];//敌机数组
this.myPlanes = [];//我方战机数组
this.createUI();
this.moveBg();
}
//853 600
Map.prototype.createUI = function() {
//1、地图的div
this.domObj = document.createElement("div");
this.domObj.style.cssText = `margin:20px auto;position: relative;width:${this.width}px;height:${this.height}px;overflow:hidden`;
document.body.appendChild(this.domObj);
//2、移动的div
this.moveBox = document.createElement("div");
this.moveBox.style.cssText = `position: absolute;
top:-1106px;
width: 480px;
height: 1706px;`;
this.domObj.appendChild(this.moveBox);
//3、图片
for(var i=0;i<2;i++){
let img01 = document.createElement("img");
img01.src = this.background;
img01.style.cssText = `display: block`;
this.moveBox.appendChild(img01);
}
//4、积分板:
this.scoreDom = document.createElement("div");
this.scoreDom.style.cssText = "position:absolute;left:0px;top:0px;width:100px;height:35px;z-index:999";
this.scoreDom.innerHTML = 0;
this.domObj.appendChild(this.scoreDom);
};
Map.prototype.moveBg = function(){
let top1 = -1106;
setInterval(()=>{
top1++;
if(top1>=-253){
top1 = -1106;
}
this.moveBox.style.top = top1+"px";
},50);
}
var instance;
return {
getInstance:function(width,height,background){
if(instance==undefined){
instance = new Map(width,height,background);
}else{
instance.width = width;
instance.height = height;
instance.background = background;
instance.domObj.style.width=this.width+"px";
instance.domObj.style.height=this.height+"px";
instance.moveBox.children[0].src=this.background;
instance.moveBox.children[1].src=this.background;
}
return instance;
}
}
})();
// 单例模式的总结
感兴趣的朋友可以使用在线HTML/CSS/Javascript前端代码调试运行工具:http://tools.jb51.net/code/WebCodeRun测试上述代码运行效果。
更多关于Javascript相关内容还可查看本站专题:《javascript面向对象入门教程》、《Javascript错误与调试技巧总结》、《Javascript数据结构与算法技巧总结》、《Javascript遍历算法与技巧总结》及《Javascript数学运算用法总结》
希望本文所述对大家Javascript程序设计有所帮助。



