javascript 设计模式之单体(Singleton)模式

单体模式介绍

    单体模式最初的定义出现于《设计模式》(艾迪生维斯理, 1994):“保证一个类仅有一个实例,并提供一个访问它的全局访问点。” javascript

         javascript是一个没有类的程序语言,固然也能够去模拟面向对象语言的类式写法。对于javascript咱们广义的去理解单体模式。

      javascript的单体模式能够带来哪些做用那?首先确定是减小了全局变量数目,javascript的全局变量是该语言的糟糕特性之一,做为一个优秀的JS程序员必定要控制全局变量的使用。其次咱们把一组相关的逻辑单元组织在一个单体中,用来划分命名空间,这样使得代码可读性更强更好维护。 java

    下面咱们学习javascript的单体模式,咱们以浏览器类型和版本为模型进行实例。 程序员

单体模式示例

简单的单体模式

最简单的单体模式实现,使用{}将相关属性方法组织起来。 设计模式

var Browser = {
type : 'Firefox',
version : '3.6',
getType : function(){return this.type;},
getVersion : function(){return this.version;}
};
console.log(Browser.getType());
console.log(Browser.getVersion());
console.log(Browser.type);

咱们已经实现了最简单是单体模式,可是此时的单体模式很脆弱,若是有人写下以下的代码,那么咱们的单体就被破坏了。 浏览器

var Browser = {
type : 'Firefox',
version : '3.6',
getType : function(){return this.type;},
getVersion : function(){return this.version;}
};
console.log(Browser.getType());
console.log(Browser.getVersion());
Browser.type = 'change';
console.log(Browser.getType());
console.log(Browser.getVersion());


此时的输出内容在的类型已经被改变掉了。咱们的浏览器类型能够随意改变,这显然不行的了。咱们须要使用私有成员来进行这个单体模式的变形。那么咱们能够使用闭包来完成私有成员的处理。 闭包

使用闭包的单体模式

var Browser = (function(){
  var type = 'Firefox';
  var version = '3.6';
  function _getType(){return type;}
  function _getVersion(){return version;}
  return {
     getType:function(){return _getType();},
     getVersion:function(){return _getVersion();}
  }
})();
console.log(Browser.getType());
console.log(Browser.getVersion());  console.log(Browser.type);


此时外面已经访问不到 Browser内部的类型与版本。下面咱们继续升级,此次咱们升级到只有使用的时候才去实例化Browser。 学习

惰性实例化单体模式

var Browser = (function(){
  var uniqueInstane;
  function constructor(){
      var type = 'Firefox';
      var version = '3.6';
      function _getType(){return type;}
      function _getVersion(){return version;}
      return {
         getType:function(){return _getType();},
         getVersion:function(){return _getVersion();}
      }
  }
  return {
      getInstance : function(){
        if(!uniqueInstane){
           console.log('instance init.');
           uniqueInstane = constructor();
        }
        return uniqueInstane;
      }
  }
})();
console.log(Browser.getInstance().getType());
console.log(Browser.getInstance().getVersion());

此时咱们此次的单体模式的示例就算是完毕了。 this

单体模式总结

    上面咱们示例了几种关于浏览器的单体模式示例。单体模式的好处在于对代码的组织,很是便于维护。结合闭包能够更好的对私有成员的操做。单体模式的弊端在于提供的是一种单点访问,可能致使模块间的强耦合。在选择这种模式的时候应该考虑到这种状况。最适合才是最好的。 spa

相关文章
相关标签/搜索