javascript 组合模式

·  阅读 79

需求:一个学校有两个班级,每个班级有两个组,小组分着上课,一起考试

不使用组合模式:

      /*
        * 需求 ,
        * 一个学校有两个班级,每个班级有两个组,小组分着上课,一起考试
        *
        *不使用组合模式
        * */
        var school = function (name) {
            this.name = name;
            var classList = [];
            this.add=function (cls) {
                classList.push(cls);
                return this;
            };
            this.getList = function () {
                return classList;
            }
        }


        var classList = function (name) {
            this.name = name;
            var groupList = [];
            this.add=function (group) {
                groupList.push(group);
                return this;
            };
            this.getList = function () {
                return groupList;
            }
        }

        var groupList = function (name) {
            this.name = name;
            var studentList = [];
            this.add=function (s) {
                studentList.push(s);
                return this;
            };
            this.getList = function () {
                return studentList;
            }
        }


        var student = function (name) {
            this.name = name;
            this.goClass = function () {
                console.log(this.name+"上课");
            };
            this.gokoshi = function () {
                console.log(this.name+"考试");
            }
        };
        var zs = new student('zs');
        var ls = new student('ls');
        var ww = new student('ww');
        var zl = new student('zl');
        var mz = new student('mz');
        var we = new student('we');
        var gd = new student('gd');
        var jg = new student('jg');
        var one = new classList("一班");
        var oneOne = new groupList("一班一组");
        oneOne.add(zs).add(ls);
        var oneTwo = new groupList("二班二组");
        oneTwo.add(ww).add(zl);
        one.add(oneOne).add(oneTwo);
        var two = new classList("二班");
        var twoOne = new groupList("二班一组");
        twoOne.add(mz).add(we);
        var twoTwo = new groupList("二班二组");
        twoTwo.add(gd).add(jg);
        two.add(twoOne).add(twoTwo);
        var scl = new school("苏屯");
        scl.add(one).add(two);

        console.log(scl.getList());
        /*
        * 一班一组上课
        * */

        var list = scl.getList();
        list.forEach(function (i) {
            if(i.name=="一班"){
                i.getList().forEach(function (j) {
                    if(j.name=="一班一组"){
                        console.log(j.getList());
                        j.getList().forEach(function (k) {
                            k.goClass();
                        })
                    }
                })
            }
        })


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
复制代码

组合模式:

  /*
        * 组合模式的实现
        *
        * */

        var Interface = function (name,methods) {
            if(arguments.length!=2){
                throw new Error('必须输入两个参数,当前个数'+arguments.length);
            };
            this.name = name; //接口名
            this.methods = []; //函数名
            for(var i=0;i<methods.length;i++){
                if(typeof methods[i] !== 'string'){
                    throw new Error('方法名参数必须为string');
                }else{
                    this.methods.push(methods[i]);
                }

            }
        };
        //定义静态方法,检验实现接口和实现类 参数object为实现类对象

        Interface.ensureImplements = function (object) {
            if(arguments.length<2){
                throw new Error('必须输入两个参数,当前个数' + arguments.length);
                return;
            };
            for(var i =1;i<arguments.length;i++){
                var interface = arguments[i];
                if(interface.constructor != Interface){
                    throw new Error("接口没有实现");
                };
                for(var j=0;j<interface.methods.length;j++){
                    var method = interface.methods[j];
                    if(!object[method] || typeof object[method]!=='function'){
                        throw new Error("接口名:"+interface.name+"方法名:"+method+"没找到");
                    }
                }
            }

        };
        function extend(subClass,superClass) {
            var f = function () {};
            f.prototype =superClass.prototype;
            subClass.prototype = new f();
            subClass.prototype.constructor = subClass;
            subClass.superclass =superClass.prototype;
            if(superClass.prototype.constructor==Object.prototype.constructor){
                superClass.prototype.constructor =superClass;
            }
        };

        var compo = new Interface('compo',['add','getList']);
        var leaf = new Interface('leaf',['goClass','gokoshi']);
        function compoList(name) {
            this.name =name;
            this.type = 1;
            var childList =[];
            this.add=function (child) {
                childList.push(child);
                return this;
            };
            this.getList=function(name){

                var toChild = [];

                if(!name){

                    childList.forEach(function (item) {
                        if(item.type==1){
                            toChild = toChild.concat(item.getList());

                        }else{
                            toChild.push(item);
                        }
                    })
                }else{
                    childList.forEach(function (unit) {
                        if(unit.name ==name){
                            if(unit.type==1){
                                toChild = toChild.concat(unit.getList());
                                return;
                            }else{
                                toChild.push(unit);
                                return;
                            }
                        }else{
                            if(unit.type==1){
                                toChild=toChild.concat(unit.getList(name))
                            }

                        }
                    })
                }
                return toChild;
            };
            this.goClass=function (name) {

                var toChild = this.getList(name);
                toChild.forEach(function (item) {
                    item.goClass();
                })
            };
            this.gokoshi=function (name) {
                var toChild = this.getList(name);
                toChild.forEach(function (item) {
                    item.gokoshi();
                })
            };
            Interface.ensureImplements(this,compo,leaf);
        }
        function leafList(name) {
            this.name =name;
            this.type = 0;
            this.add=function (child) {
                throw new Error("该方法不能实现");
            };
            this.getList=function(name){
                if(this.name==name){
                    return this;
                }else {
                    return "";
                }

            };
            this.goClass=function () {
               console.log(this.name+"上课");
            };
            this.gokoshi=function () {
                console.log(this.name+"考试");
            };
            Interface.ensureImplements(this,compo,leaf);
        }


        var zs = new leafList('zs');
        var ls = new leafList('ls');
        var ww = new leafList('ww');
        var zl = new leafList('zl');
        var mz = new leafList('mz');
        var we = new leafList('we');
        var gd = new leafList('gd');
        var jg = new leafList('jg');
        var one = new compoList("一班");
        var oneOne = new compoList("一班一组");
        oneOne.add(zs).add(ls);

        var oneTwo = new compoList("二班二组");
        oneTwo.add(ww).add(zl);
        one.add(oneOne).add(oneTwo);
        var two = new compoList("二班");
        var twoOne = new compoList("二班一组");
        twoOne.add(mz).add(we);
        var twoTwo = new compoList("二班二组");
        twoTwo.add(gd).add(jg);
        two.add(twoOne).add(twoTwo);
        var scl = new compoList("苏屯");

        scl.add(one).add(two);
复制代码
分类:
前端
标签:
分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改