Linux+Python+Java饥人谷技术博客程序员

设计模式的梗概

2016-11-03  本文已影响168人  犯迷糊的小羊

什么是设计模式

一般来说,创建不同类型的对象的套路称之为设计模式(Pattern Design)

常见的设计模式包括:

上面这几种基本的设计模式并不是孤立存在的,可以结合一起使用。

1.单例模式

极简方式

优点:明白晓畅、简单快捷
缺点:缺乏封装,成员暴露,初始化时占用资源

var singleton = {
    attr:1,
    method:function(){
      return this.attr
    }
}
var ex1 = singleton;
var ex2 = singleton;

闭包方式

优点:解决成员暴露和初始化占用资源问题

var Substance = (function(){
  var unique;
  function init(){
    var type;
    return {
      setType: function(t){
        console.log(type = t)
      }
    }
  }
  
  return {
    getInstance:function(){
      if(!unique){
        unique = init();
      }
        return unique
     
    }
  }
})()
var Adam = Substance.getInstance();
var Eve = Substance.getInstance();
Adam.setType('Man')//Man

【mask】

2.工厂模式

function Animals (opts){
  var animals = {
    name:opts.name,
    type:opts.type
  };
  animals.sayName = function(){
    console.log(this.name)
  } 
  return animals
}
var tony = Animals({name:'tony',type:'dog'})
console.log(tony.type)
tony.sayName()
var sina = Animals({name:'sina',type:'cat'})
console.log(sina.type)
sina.sayName()

【dialog】

3.构造函数模式

function Person(name,age){
  this.name = name;
  this.age = age
}
Person.prototype.sayName = function(){
  console.log(this.name)
}
var Kobe = new Person('Kobe','36');
var James = new Person('James','30');
Kobe.sayName()
James.sayName()

【dialog】

4.混合模式

function Animals(race,activity){
  this.race = race;
  this.activity = activity;
};
Animals.prototype.getActivity = function(){
  console.log(this.activity)
};

var Dog = function(race,activity){
  Animals.call(this,race,activity);
  this.voice = 'wow...'
};
Dog.prototype = Object.create(Animals.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.spark = function(){
  console.log(this.voice);
};
var Cat = function(race,activity){
  Animals.call(this,race,activity);
  this.voice = 'miao...'
};
Cat.prototype = Object.create(Animals.prototype);
Cat.prototype.constructor = Dog;
Cat.prototype.spark = function(){
  console.log(this.voice);
};
var tony = new Dog('dog','run');
console.log(tony.race);
console.log(tony.activity);
console.log(tony.voice);
tony.getActivity();
tony.spark();

var wiky = new Cat('cat','sleep')
wiky.getActivity();
wiky.spark();

【混合模式】

5.模块模式

var Calculator = (function(){
  //闭包隐藏私有变量
  var PI = 3.14
  function circle(r){
    return PI*r*r
  } 
  return {
    //暴露接口以供调用
      circle:circle
  }  
})();
console.dir(Calculator.circle(3))//28.259999999999998
console.dir(PI)//ReferenceError: PI is not defined

【模块化模式】

6.发布订阅模式

var EventManger = (function(){
  var event = {}; 
  function on(eventName,callback){
    event[eventName] = event[eventName] || [];
    event[eventName].push({
      callback:callback,
    })
    return this
  };  
  function fire(eventName,data){
    if(!event[eventName]){return}
    var subscribers = event[eventName];
    var len = subscribers.length;
    while(len--){
      subscribers[len].callback(data)
    }
    return this;
   
  };
  
  function off(eventName){
    if(!event[eventName]){return}
    event[eventName].splice(0,1);
   return this
  
  }
  return {
    on:on,
    fire:fire,
    off:off
  }
})();

EventManger.on('text:change',function(val){
  console.log('text:change...now val is '+val);
});

EventManger.fire('text:change','hungerVallery');

EventManger.fire('text:change','stay hungry');

EventManger.off('text:change');

EventManger.fire('text:change','stay foolish');

【发布订阅模式】

上一篇下一篇

猜你喜欢

热点阅读