高级6 设计模式

1.写出 构造函数模式、混合模式、模块模式、工厂模式、单例模式、发布订阅模式的范例。

    /*—————————————————————————————构造函数————————————————————————————————*/
    function Person(name,age) {
        this.name =name
        this.age = age
    }
    Person.prototype.sayName = function() {
        return this.name
    }
    var student = new Person('xiaoming',12)

    /*————————————————————————————— 工厂模式————————————————————————————————*/
    function createPerson(name) {
        var person={
            name:name,
            sayName:function() {
                console.log(this.name)
            }
        };
        return person;
    }
    createPerson('zhangsan')
    createPerson('lisi')
    
    /*——————————————————————————————单例模式————————————————————————————————*/
    var People = (function() {
      var instance
      function init(name) {
        return {
            name:name
        };
      }
      return {
        createPeople: function(name) {
            if (!instance) {
                instance = init(name)
            }
            return instance;
        }
      }
    }())
    People.createPeople('zhangsan')
    
/*————————————————————————————混合模式————————————————————————————————*/
var Person = function(name, age) {
    this.name = name;
    this.age = age;
};
Person.prototype.sayName = function(){
  console.log(this.name);
}
var Student = function(name, age,  score) {
    Person.call(this, name, age);
    this.score = score;
};
//Student.prototype = Object.create(Person.prototype);
Student.prototype = create(Person.prototype);
function create (parentObj){
    function F(){}
    F.prototype = parentObj;
    return new F();
};
Student.prototype.sayScore = function(){
  console.log(this.score);
}
var student = new Student("饥人谷", 28, 99);
console.log(student);

/*—————————————————————————————模块模式————————————————————————————————*/
var Person = (function(){
    var name = 'ruoyu';
    function sayName(){
        console.log(name);
    }
    
    return {
        name: name,
        sayName: sayName
    }
})()

/*———————————————————————————订阅发布模式————————————————————————————————*/
var PubSub = function() {
    this.eventPool = {}
}
PubSub.prototype.on = function(topicName,callback) {
    var topic = this.eventPool[topicName]
    if (!topic) {
        this.eventPool[topicName] = []
    }
    this.eventPool[topicName].push(callback)
}
PubSub.prototype.trigger = function() {
    var _arg = [].slice.call(arguments)
    var topicName = _arg.shift()
    var callbackArg = _arg
    var topicCallback = this.eventPool[topicName]
    if (topicCallback) {
        topicCallback.forEach(function(callback){
          callback.apply(this,callbackArg)
        })
    }
}
PubSub.prototype.off = function() {
    delete this.eventPool[topicName]
}

2.使用发布订阅模式写一个事件管理器,可以实现如下方式调用

var PubSub = function() {
    this.eventPool = {}
}
PubSub.prototype.on = function(topicName,callback) {
    var topic = this.eventPool[topicName]
    if (!topic) {
        this.eventPool[topicName] = []
    }
    this.eventPool[topicName].push(callback)
}
PubSub.prototype.fire = function() {
    var _arg = [].slice.call(arguments)
    var topicName = _arg.shift()
    var callbackArg = _arg
    var topicCallback = this.eventPool[topicName]
    if (topicCallback) {
        topicCallback.forEach(function(callback){
          callback.apply(this,callbackArg)
        })
    }
}
PubSub.prototype.off = function(topicName) {
    delete this.eventPool[topicName]
}
var Event = new PubSub()
Event.on('change', function(val){
    console.log('change...  now val is ' + val);  
});
Event.fire('change', '饥人谷');
Event.off('changer');
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,831评论 25 709
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,964评论 2 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 美人泪 忧在头 负心仇 莫回首
    青玄_6230阅读 282评论 0 0
  • 大二那年开学,纠结学生会要不要干下去,社团要不要继续参加,问一个朋友,他问了下我们学 校的名字,然后淡淡地说,全国...
    蛀书小强阅读 344评论 0 0