zoukankan      html  css  js  c++  java
  • 设计模式

    一、单例模式(singleton)
     
    1.概念:单个实例,只有一个对象,多次创建,返回同一个对象;
         
     1    面向对象的实例对象
     2            function Person(name,age){
     3              if(!Person.instance){
     4                   Person.instance={
     5                       name : "李四",
     6                       age : "12",
     7                       showName : function(){
     8                            return this.name;
     9                       },
    10                       showAge : function(){
    11                            return this.age;
    12                       }
    13                   }
    14              }
    15              return Person.instance;
    16          }
    17          var ps2 = new Person();
    18          var ps3 = new Person();
    19  
    20          alert(ps2==ps3)    //true
    2.创建方式:
          
     1   01字面量创建方式:var ps1 = {
     2                              "name" :"张三";
     3                              "age" :15;
     4                              showName:function(){
     5                                   return this.name;
     6                              }
     7                              showAge:function(){
     8                                   return this.age;
     9                              }
    10                         }
    11   02:function Person(name,age){
    12                  this.name = name;
    13                  this.age = age;
    14              }
    15             Person.prototype.showName =function(){
    16                  return this.name;
    17            }
    18            Person.prototype.showAge =function(){
    19                  return this.age;
    20            }
    21            var ps2 = new Person("张三",46);
    22            var ps3 = new Person("张三",56);
    23            alert(ps2==ps3)   //false 
    面试题
           
    1 alert([]==[]);             //false
    2 alert([] == ![])              //true
    3 alert(typeof []);             //object
    二、代理模式(微商)
     案例:马蓉和宝宝的故事
        
     1    //花店
     2      function Flower(type){
     3          this.type=type;
     4      }
     5      //宝宝
     6      var Baobao={
     7          sendFlower:function(target){        //给目标者送花
     8               var flower = new Flower("狗尾巴花");    //买花
     9               target.receiveFlower(flower);           //目标者收到花
    10          }
    11      }
    12      //代理送花
    13      var Songzhe={
    14          receiveFlower:function(flower){
    15               Marong.receiveFlower(flower);
    16          }
    17      }
    18      var Marong={
    19          receiveFlower:function(flower){     //收花的人
    20               alert("收到一束:"+flower.type+",祝你幸福");
    21          }
    22      }
    23  
    24      Baobao.sendFlower(Songzhe);
    三、适配器模式
     
    两个软件通过接口  可以同时运转(手机和充电器)
     
     
    四、工厂模式
    五、组合模式
    六、策略模式
       解决问题的不同方法
    七、观察者模式
     
      又叫  发布-订阅式 ------> 群发信息
     
     
     
  • 相关阅读:
    【题解】Killer Names($O(nlog n)$做法)
    【瞎讲】类欧几里得入土教程
    【题解】SDOI2010所驼门王的宝藏(强连通分量+优化建图)
    【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)
    【题解】LOJ6060 Set(线性基)
    【题解】CF1056F Write the Contest(三分+贪心+DP)
    【题解】多少个$1$(exBSGS)
    【题解】幼儿园篮球题(范德蒙德卷积+斯特林+NTT)
    【题解】P1373 小a和uim之大逃离
    【题解】地精部落(DP)
  • 原文地址:https://www.cnblogs.com/Makeprogresstogether/p/8011436.html
Copyright © 2011-2022 走看看