JavaScript組合設計模式--改進引入案例分析
本文實例講述了JavaScript組合設計模式--改進引入案例。分享給大家供大家參考,具體如下:
對于組合設計模式:
(1)組合模式中把對象分為兩種(組合對象,和葉子對象) (2)組合對象和葉子對象實現:同一批操作 (3)對組合對象執行的操作可以向下傳遞到葉子節點進行操作 (4)這樣就會弱化類與類之間的耦合 (5)他常用的手法是把對象組合成屬性結構的對象
根據組合模式的這些特性我們改寫代碼如下:
由于用到了接口檢驗所以我們先引入接口文件代碼
//定義一個靜態方法來實現接口與實現類的直接檢驗//靜態方法不要寫出Interface.prototype ,因為這是寫到接口的原型鏈上的//我們要把靜態的函數直接寫到類層次上//定義一個接口類var Interface=function (name,methods) {//name:接口名字 if(arguments.length<2){ alert('必須是兩個參數') } this.name=name; this.methods=[];//定義一個空數組裝載函數名 for(var i=0;i<methods.length;i++){ if(typeof methods[i]!='string'){ alert('函數名必須是字符串類型'); }else { this.methods.push( methods[i]); } }};Interface.ensureImplement=function (object) { if(arguments.length<2){ throw new Error('參數必須不少于2個') return false; } for(var i=1;i<arguments.length;i++){ var inter=arguments[i]; //如果是接口就必須是Interface類型 if(inter.constructor!=Interface){ throw new Error('如果是接口類的話,就必須是Interface類型'); } //判斷接口中的方法是否全部實現 //遍歷函數集合 for(var j=0;j<inter.methods.length;j++){ var method=inter.methods[j];//接口中所有函數 //object[method]傳入的函數 //最終是判斷傳入的函數是否與接口中所用函數匹配 if(!object[method]||typeof object[method]!='function' ){//實現類中必須有方法名字與接口中所用方法名相同throw new Error('實現類中沒有完全實現接口中的所有方法') } } }}
(1)統一接口
var composite=new Interface('composite',['getChildByName','add']);//側重點獲取子var student=new Interface('composite',['goToClass','finishClass']);//側重點為每個對象的實現
(2)定義組合類
var compositeObj=function (name) { this.name=name; this.type='com';//默認是組合類 var childs=new Array();//得到相關的所有孩子節點 this.getChildByName=function (name) { //涉及到遞歸 var toChilds=new Array(); if(!name){ for(var i=0;i<childs.length;i++){if(childs[i].type=='com'){ toChilds=toChilds.concat(childs[i].getChildByName())}else { toChilds.push(childs[i]);} } }else { for (var i = 0; i < childs.length; i++){ if(childs[i].name==name){ if(childs[i].type=='com'){ toChilds=toChilds.concat(childs[i].getChildByName()); break; }else { toChilds.push(childs[i]); break; } }else { if(childs[i].type == 'com'){ toChilds =toChilds.concat(childs[i].getChildByName(name)); } } } } return toChilds; }; //增加子節點 this.add=function (child) { childs.push(child); return this; }; //去上課this.goToClass=function (name) {var toChilds=this.getChildByName(name); for(var i=0;i<toChilds.length;i++){ toChilds[i].goToClass(); } }; //下課 this.finishClass=function (name) { var toChilds=this.getChildByName(name); for(var i=0;i<toChilds.length;i++){toChilds[i].finishClass(); } }; Interface.ensureImplement(this,composite,student); };
(3)定義葉子類
var studentObj=function (name) { this.name=name; this.type='student';//默認是葉子 //得到所有孩子節點 this.getChildByName=function (name) { if(this.name==name){ return this; }else { return null; } } //增加子節點 this.add=function (child) { throw new Error('add 不成被初始化(在葉子了中)') } //去上課 this.goToClass = function(name){ document.write(this.name +' 去上課<br>'); } //下課 this.finishClass = function(name){ document.write(this.name +' 下課<br>'); } Interface.ensureImplement(this,composite,student); }
(4)應用---將學校,班級,組,學生關聯起來
var astudent=new studentObj('我是a同學'); var bstudent=new studentObj('我是b同學'); var cstudent=new studentObj('我是c同學'); var dstudent=new studentObj('我是d同學'); var estudent=new studentObj('我是e同學'); var fstudent=new studentObj('我是f同學'); var gstudent=new studentObj('我是g同學'); var hstudent=new studentObj('我是h同學'); var istudent=new studentObj('我是i同學'); var one = new compositeObj('一班'); var oneOne = new compositeObj('一班一組'); oneOne.add(astudent).add(bstudent); var oneTwo = new compositeObj('一班二組'); oneTwo.add(cstudent).add(dstudent); one.add(oneOne).add(oneTwo); var two = new compositeObj('二班'); var twoOne = new compositeObj('二班一組'); twoOne.add(estudent).add(fstudent); var twoTwo = new compositeObj('二班二組'); twoTwo.add(gstudent).add(hstudent).add(istudent) two.add(twoOne).add(twoTwo); var usPcat = new compositeObj('組合設計模式培訓學校'); usPcat.add(one).add(two);
(5)客戶端調用API,只需要簡單的安排去上課即可,也就是客戶端只需要寫去上課的代碼即可
usPcat.goToClass();document.write('-------------------------<br>');usPcat.goToClass('一班');document.write('-------------------------<br>');usPcat.goToClass('二班一組');
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《javascript面向對象入門教程》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章:
