1 <html> 2 <head> 3 <title>javascript高级语法20-责任链模式</title> 4 </head> 5 <body> 6 <script> 7 /*责任链模式 8 * 消除发送者与接收者之间的耦合。 9 * 1.责任链的发送者知道链的入口是谁 10 * 2.每一个链节点知道自己的下一个节点是谁 11 * 3.每一个链的传入和传出值是一样的; 12 */ 13 14 //扩展filter函数 15 Function.prototype.method = function(name,fn){ 16 this.prototype[name] = fn; 17 return this; 18 } 19 if(!Array.prototype.filter){ 20 Array.method("filter",function(fn,obj){ 21 var scope = this.Obj || window; 22 var a = []; 23 for(var i=0;i<this.length;i++){ 24 if(!fn.call(scope,this[i],i,this)){ 25 continue; 26 } 27 a.push(this[i]); 28 } 29 //返回过滤好的数据 30 return a; 31 }) 32 } 33 34 function demo(){ 35 //扩展window 36 window.setSuccessor = function(after,before){ 37 after.successor = before; 38 } 39 //用于拷贝字符串的方法 20*"-" = "------------------" 40 window.copyStr = function(num,str){ 41 var newStr = ''; 42 for(var i=0;i<num;i++){ 43 newStr += str; 44 } 45 return newStr; 46 } 47 //转换数组中的对象为字符串 48 var showStr = function(arr){ 49 var newStr = ""; 50 for(var i=0;i<arr.length;i++){ 51 newStr += JSON.stringify(arr[i]); 52 } 53 return newStr; 54 } 55 56 //书店找书和加书两个需求 57 //用户可以输入关键字找书(书号,作者,名称) 58 //不同书架管理员管理自己的书籍添加任务 59 60 //图书类 61 var Book = function(id,name,author,type){ 62 this.id = id; 63 this.name = name; 64 this.author = author; 65 this.type = type; 66 } 67 //书店 68 var myBookShop = (function(){ 69 //书架 70 var jsBooks = new Array(); //JS书架 71 var cBooks = new Array(); //C语言书架 72 var pythonBooks = new Array(); //python书架 73 //内部类1 74 function addJSBook(book){ 75 if(book.type == "js"){ 76 jsBooks.push(book); 77 }else{ 78 //负责传递到下一个功能 79 addJSBook.successor(book); 80 } 81 } 82 //内部类2 83 function addPythonBook(book){ 84 if(book.type == "py"){ 85 pythonBooks.push(book); 86 }else{ 87 //负责传递到下一个功能 88 addPythonBook.successor(book); 89 } 90 } 91 //内部类3 92 function addCBook(book){ 93 if(book.type == "c"){ 94 cBooks.push(book); 95 }else{ 96 //负责传递到下一个功能 97 addCBook.successor(book); 98 } 99 } 100 //设置责任链 101 setSuccessor(addJSBook,addPythonBook); 102 setSuccessor(addPythonBook,addCBook); 103 104 //查询书籍 105 var bookList = null; 106 function findBookById(keyword){ 107 if(!bookList){ 108 bookList = jsBooks.concat(cBooks).concat(pythonBooks); 109 } 110 var book = new Array(); 111 book = bookList.filter(function(book){ 112 if(book.id.indexOf(keyword) != -1){ 113 return true; 114 }else{ 115 return false; 116 } 117 }); 118 //进行链式查询 119 return book.concat(findBookById.successor(keyword,book)); 120 } 121 function findBookByName(keyword,book){ 122 book = bookList.filter(function(book){ 123 if(book.name.indexOf(keyword) != -1){ 124 return true; 125 }else{ 126 return false; 127 } 128 }); 129 return book; 130 } 131 //规划责任链 132 setSuccessor(findBookById,findBookByName); 133 134 //真正的图书馆类 135 return function(){ 136 this.addBook = function(book){ 137 if(book instanceof Book){ 138 //因为我知道谁是链的入口。可以这么写 139 addJSBook(book); 140 } 141 } 142 this.findBook = function(keyword){ 143 return findBookById(keyword); 144 } 145 this.showBooks = function(){ 146 //document.write("c语言类图书"+cBooks.toSource()+"<br>") 147 //这里的toSource()方法只有火狐支持,所以我全局定义了showStr方法 148 document.write("js类图书"+showStr(jsBooks)+"<br>") 149 document.write("c语言类图书"+showStr(cBooks)+"<br>") 150 document.write("Python类图书"+showStr(pythonBooks)+"<br>") 151 document.write(copyStr(60,"-")+"<br>"); 152 } 153 } 154 })(); 155 //添加book 156 var pb = new myBookShop(); 157 pb.addBook(new Book("00101","Extjs","zhangsan","js")); 158 pb.addBook(new Book("00102","cprimer","jim","js")); 159 pb.addBook(new Book("00103","c#","chance","c")); 160 pb.addBook(new Book("00201","c++","chance","c")); 161 pb.addBook(new Book("00202","python","py","py")); 162 pb.addBook(new Book("00203","py实战","杨国正","py")); 163 //展示 164 pb.showBooks(); 165 //查询 166 document.write(showStr(pb.findBook("02"))+"<br>"); 167 document.write(copyStr(60,"-")+"<br>"); 168 document.write(showStr(pb.findBook("c"))); 169 } 170 demo(); 171 </script> 172 </body> 173 </html>