设计模式

一、单例模式(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);
三、适配器模式
 
两个软件通过接口  可以同时运转(手机和充电器)
 
 
四、工厂模式
五、组合模式
六、策略模式
   解决问题的不同方法
七、观察者模式
 
  又叫  发布-订阅式 ------> 群发信息
 
 
 
posted @ 2017-12-09 14:38  夏至有你  阅读(127)  评论(0编辑  收藏  举报