美文网首页Web
JS中的常用的设计模式

JS中的常用的设计模式

作者: 追逐_chase | 来源:发表于2019-08-20 14:27 被阅读15次
    web.jpeg

    常见的设计模式有23种,其中就包括 工厂设计模式,其实就是 批量生产,批量制作,单例设计模式 只在内存开辟一个内存空间,观察者设计模式其实就是 广播通知,一个订阅者订阅发布者的信息,一旦这个发布者发布信息,订阅者就会收到

    核心的步骤:
    1.提供一个父构造函数 (工厂)
    2.设置父构造函数的原型对象(产品公共的东西)
    3.设置父构造函数的静态工厂方法 (生成车间)
    3.1 接受传入的参数(产品的类型)
    3.2 判断是否支持生产
    3.3 设置子构造函数的原型对象
    3.4 利用子构造函数创建对象
    3.5 返回创建的对象
    4.定制合作伙伴
    5.利用父构造函数的静态工厂方法创建对象

    //1.创建父构造函数
        function Phone() { };
     //2设置公用的方法
        Phone.prototype.logoDes = function () {
            console.log(this.des);
        };
    
    //3.提供创建的方法
        Phone.make = function (type) {
    
            //判断是否为合作伙伴
            if (typeof Phone[type] != "function") {
                throw "目前该产品没有合作哦";
            }
            //设置自构造函数的原型对象为 父构造函数( 可以获取 父构造函数的 属性和方法)
            Phone[type].prototype = new Phone();
    
            var product = new Phone[type]();
    
            return product;
    
        }
    
    //4.定制合作对象
        Phone.iphone = function () {
            this.des = "我是苹果合作独享";
        };
        Phone.vovo = function () {
            this.des = "步步高手机";
        }
        Phone.oppo = function () {
            this.des = "充电5分钟,通话2小时";
        }
    //5.使用构造函数的静态工厂方法创建对象,就是批量生产
    
        var iphone = Phone.make("iphone");
        iphone.logoDes();
    
    
    单例设计模式
    //惰性函数 创建对象
    
    function Person(){
        var instance; // undefinded
        Person = function(){
        //第一次执行 
        return instance;
        }
        //设置新构造函数的原型对象是 旧构造函数的一个实例
        Person.prototype = this;
        // 使用新构造函数 创建一个对象赋值给instace
        instance = new Person();
    
        //修改 构造器函数
        instance.constructor = Person;
        //设置属性和方法
        instace.name = "CC";
        return instance;
    
    }
    
    var per1 = new Person();
     Person.prototype.des = "des"
    
    var per2 = new Person();
    Person.prototype.hi = "hi";
    
     console.log(per1 == per2);
    
     console.log(per1.des);
     console.log(per2.des);
    
     console.log(per1.hi);
     console.log(per2.hi);
     
     
    
    console.log(per1.constructor == Person);
    
    
    观察者模式
    <script>
     
      var publisher ={
          addUser:function(fn,type){
             var type = type || "eat";
             //如果user里面的 发布的状态 没有定义,动态添加一个数组,来存储这个type类型的状态
             if(this.user[type] == undefined){
                 this.user[type] = [];
             }
             //如果 对应的不是方法 抛出异常
             if(typeof fn != "function"){
                 throw "不支持";
             }
             //添加对应的方法
             this.user[type].push(fn);
    
    
          },
          removeUser:function(fn,type){
                this.publish(type,fn);
          },
          publish:function(type,fn){
              var type = type || "eat";
         
              if(!Array.isArray(this.user[type])){
                  throw "存在这个。。。";
              }
              
            for(var i = 0; i < this.user[type].length; i ++){
                    //判断fn这个方法是不是 移除方法
                    if(typeof fn == "function"){
                        //移除观察者
                        this.user[type].splice(i,1);
                    }else{
                        //调用数组里面的方法---监听  
                        this.user[type][i]();
                    }
            }
          }
      };
    
    
      //1.创建发布者
    
      var sender = {
          eat:function(){
              this.publish("eat");
          },
          //订阅别人的发布 状态
          sender_lol:function(){
            console.log("你又开始玩LOL了,😁");
            
          }
    
    
      };
      //2.属性继承
    
      function makePublisher(obj){
          for(var key in publisher){
              //拷贝里面共用的方法
              if(publisher.hasOwnProperty(key) && (typeof publisher[key] == "function")){
                    obj[key] = publisher[key];
              }
          }
          //这个属性 对象 用来存储 (key:[])key代表 发布的 方法  []存储订阅者的
          obj.user = {eat:[]};
      }
    
      //继承 模板
      makePublisher(sender);
    
    
      //2.创建 订阅者
    
      var jack = {
          jack_eat:function(){
              console.log("订阅 sender 发布的 方法");
              
          },
    
          lol:function(){
             this.publish("lol");
              
          }
      }
    
      //继承模板
      makePublisher(jack);
    
      //3.注册订阅者
    
      sender.addUser(jack.jack_eat,"eat");
    
      jack.addUser(sender.sender_lol,"lol");
      
      // jack 和 sender 互为订阅者
    
      //4.发布订阅/通知
    
      sender.eat();
    
      jack.lol();
    
      
      jack.removeUser(sender.sender_lol,"lol");
      
      jack.lol();
    
    
    
    
    
    </script>
    
    
    
    

    相关文章

      网友评论

        本文标题:JS中的常用的设计模式

        本文链接:https://www.haomeiwen.com/subject/rrkpsctx.html