js39---組合模式,查找遍歷樹
阿新 • • 發佈:2017-05-23
arr div composite ini 2個 需求 沒有 error nts
/** *有這樣一個需求 *有一個學校有2個班(一班,二班) *每個班級分2個小組(一班一組,一班二組,二班一組,二班二組) *學校計算機教室有限,每一個小組分著來上課. *考試的時候大家一起考 *請用程序來模擬這個需求 */ (function(){ //不用組合模式 //學校類 var school = function(name){ this.name = name; //班級 var classes = new Array(); this.addClasses = function(cla){ classes.push(cla);return this; } this.getClasses = function(){ return classes; } } //班級類 var classes = function(name){ this.name = name; var groups = new Array(); this.getGroup = function(){ return groups; } this.addGroup = function(group){ groups.push(group); return this; } } //組 var group = function(name){ this.name = name; var students = new Array(); this.getStudent = function(){ return students; } this.addStudent = function(stu){ students.push(stu);return this; } } //學生類 var student = function(name){ this.name = name; this.goToClass = function(){ document.write(this.name+" 去上課"); } this.finishClass = function(){ document.write(this.name+" 下課"); } } //測試 var a = new student("a"); var b = new student("b"); var c = new student("c"); var d = new student("d"); var e = new student("e"); var f = new student("f"); var g = new student("g"); var h = new student("h"); var i = new student("i"); var one = new classes("一班"); var oneOne = new group("一班一組"); oneOne.addStudent(a).addStudent(b); var oneTwo = new group("一班二組"); oneTwo.addStudent(c).addStudent(d); one.addGroup(oneOne).addGroup(oneTwo); var two = new classes("二班"); var twoOne = new group("二班一組"); twoOne.addStudent(e).addStudent(f); var twoTwo = new group("二班二組"); twoTwo.addStudent(g).addStudent(h).addStudent(i) two.addGroup(twoOne).addGroup(twoTwo); var usPcat = new school("波斯卡特計算機培訓學校"); usPcat.addClasses(one).addClasses(two); //調用 就寫一個 一班一組去上課 var classes= usPcat.getClasses(); for (var i = 0; i < classes.length; i++) { if(classes[i].name == "一班"){ for (var j = 0; j < classes[i].getGroup().length; j++) { // document.write(classes[i].getGroup()[j]) if(classes[i].getGroup()[j].name == "一班一組"){ var s = classes[i].getGroup()[j].getStudent(); for (var k = 0; k < s.length; k++) { s[k].goToClass(); } } } } } //我快些吐了 //這種方法一定不是和業務的擴展 })()
/** * 組合模式 */ (function(){ //統一接口 var composite = new Interface("composite",["getChildByName","add"]); var student= new Interface("composite",["goToClass","finishClass"]); //定義組合類 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.ensureImplements(this,composite,student) } //定義葉子類 var studentObj = function(name){ this.name = name; this.type = "stu";//默認是葉子 //得到相關的所有孩子節點 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.ensureImplements(this,composite,student) } //測試 var a = new studentObj("a"); var b = new studentObj("b"); var c = new studentObj("c"); var d = new studentObj("d"); var e = new studentObj("e"); var f = new studentObj("f"); var g = new studentObj("g"); var h = new studentObj("h"); var i = new studentObj("i"); var one = new compositeObj("一班"); var oneOne = new compositeObj("一班一組"); oneOne.add(a).add(b); var oneTwo = new compositeObj("一班二組"); oneTwo.add(c).add(d); one.add(oneOne).add(oneTwo); var two = new compositeObj("二班"); var twoOne = new compositeObj("二班一組"); twoOne.add(e).add(f); var twoTwo = new compositeObj("二班二組"); twoTwo.add(g).add(h).add(i) two.add(twoOne).add(twoTwo); var usPcat = new compositeObj("波斯卡特計算機培訓學校"); usPcat.add(one).add(two); //客戶端調用API usPcat.goToClass(); document.write("-------------------------<br>"); usPcat.goToClass("一班"); document.write("-------------------------<br>"); usPcat.goToClass("二班一組"); document.write("-------------------------<br>"); usPcat.goToClass("a"); })()
js39---組合模式,查找遍歷樹