java Script 组合模式 简单实例

<!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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值