组合模式简单实现

it2025-03-27  21

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> <script defer="defer" charset="UTF-8" type="text/javascript"> (function() { Array.prototype.each = function(fn) { try { this.i || (this.i = 0); if(this.length > 0 && fn.constructor == Function) { while(this.i < this.length) { var e = this[this.i]; if(e && e.constructor == Array) { e.each(fn); } else { fn.apply(e, [e]); } this.i++ } this.i = null; } } catch(ex) { } return this } var Interface = function(name, methods) { //1.判断实参的个数 if(arguments.length != 2) { throw new Error("参数不够"); } this.name = name; this.methods = []; for(var i = 0, len = methods.length; i < len; i++) { if(typeof methods[i] !== "string") { throw new Error("方法名必须是String类型"); } this.methods.push(methods[i]); } } Interface.ensureImplements = function(object) { if(arguments.length < 2) { throw new Error("参数个数不够") } for(var i = 1, len = arguments.length; i < len; i++) { var interfaceName = arguments[i]; if(interfaceName.constructor !== Interface) { throw new Error("接口类型不匹配"); } for(var j = 0; j < interfaceName.methods.length; j++) { var methodName = interfaceName.methods[j]; if(!object[methodName] || typeof object[methodName] !== "function") { throw new Error("方法不存在"); } } } } window.Interface = Interface; })(); (function() { var Org = function(name) { this.name = name; this.depts = []; } Org.prototype = { constructor: Org, addDepts: function(childDept) { this.depts.push(childDept); return this; }, getDepts: function() { return this.depts; } } var Dept = function(name) { this.name = name; this.persons = []; } Dept.prototype = { constructor: Org, addPersons: function(childPersons) { this.persons.push(childPersons); return this; }, getPersons: function() { return this.persons; } } var Person = function(name) { this.name = name; } Person.prototype = { constructor: Person, work: function() { console.info("努力工作的" + this.name); }, sleep: function() { console.info("睡觉中的" + this.name); } } var a = new Person("a"); var b = new Person("b"); var c = new Person("c"); var d = new Person("d"); var e = new Person("e"); var f = new Person("f"); var A = new Dept("A"); A.addPersons(a).addPersons(b).addPersons(c); var B = new Dept("B"); B.addPersons(d).addPersons(e).addPersons(f); var Aa = new Org("Org"); Aa.addDepts(A).addDepts(B); //最终的节点落实在人上面 如果是部门呢?如果部门下面 又创建了部门呢? OMG 不敢想了,此时 组合模式就诞生了。 for(var i = 0, depts = Aa.getDepts(); i < depts.length; i++) { for(var j = 0, persons = depts[i].getPersons(); j < persons.length; j++) { if(persons[j]["name"] === "e") { //persons[j].work(); } } } })(); (function() { /* * 应用场景(节点随便添加完全无视) 总公司--->上海分公司--->财务部门--->财务A * --->财务B * --->销售部门--->销售A * --->销售B * --->财务部门--->财务A * --->财务B --->销售部门 --->销售A */ var CompositeInterface = new Interface("CompositeInterface", ["addChild", "getChild"]); var LeafInterface = new Interface("LeafInterface", ["work", "sleep"]); //组合对象 var Composite = function(name) { this.name = name; this.type = "Composite"; this.children = []; }; Composite.prototype = { constructor: Composite, addChild: function(child) { this.children.push(child); return this; }, getChild: function(name) { var elements = []; var pushLeaf = function(item) { if(item.type === "Composite") { item.children.each(arguments.callee); } else if(item.type === "Leaf") { elements.push(item); } } if(name && this.name !== name) { this.children.each(function(item) { if(item.name === name && item.type === "Composite") { item.children.each(pushLeaf); } if(item !== name && item.type === "Composite") { item.children.each(arguments.callee); } if(item.name === name && item.type === "Leaf") { elements.push(item); } }); } else { this.children.each(pushLeaf); } return elements; }, work: function(name) { var childObjects = this.getChild(name); for(var i = 0; i < childObjects.length; i++) { childObjects[i].work(); } }, sleep: function(name) { var childObjects = this.getChild(name); for(var i = 0; i < childObjects.length; i++) { childObjects[i].sleep(); } } } //叶子对象 var Leaf = function(name) { this.name = name; this.type = "Leaf"; }; Leaf.prototype = { constructor: Leaf, addChild: function(child) { throw new SyntaxError("最底层节点 无法在添加子节点"); }, getChild: function(name) { if(this.name === name) { return this; } return null; }, work: function() { console.info("努力工作的" + this.name); }, sleep: function() { console.info("睡觉中的" + this.name); } } var a = new Leaf("a"); var b = new Leaf("b"); var c = new Leaf("c"); var d = new Leaf("d"); var e = new Leaf("e"); var f = new Leaf("f"); var A = new Composite("A"); A.addChild(a).addChild(b).addChild(c); var B = new Composite("B"); B.addChild(d).addChild(e).addChild(f); var Aa = new Composite("Org"); Aa.addChild(A).addChild(B); /*调用规则*/ Aa.work(); Aa.work("A"); Aa.work("b"); })(); </script> </html>

 

转载于:https://www.cnblogs.com/ctrek/p/6367475.html

最新回复(0)