JavaScript combination Mode---introduced

Source: Internet
Author: User
Tags addgroup

First of all:
Use an example to introduce a combination pattern that needs to be
(1) There is a school with 2 classes (one class, two classes)
(2) Each class is divided into 2 groups (one Class one group, one class two groups, the second class one group, the second class two group)
(3) school computer classrooms are limited and each group is divided into classes.

Then: According to the requirements we can easily see that this involves schools, classes, groups, and students a total of 4 categories
Using a program  to simulate these 4 classes as,
(1) School class
  var school=Function (name) {        this. name=name;          var classes=New  Array ();          this. addclasses=function (CLA) {            classes.push (classe);         }          this. getclass=function () {             return  classes;         }    }

(2) class

// Class Classes    var  classes=Function (name) {        this. Name = name;          var New Array ();         this. AddGroup = function (group) {            Groups.push (group);             return  This ;        };         this. GetGroups = function () {            return  groups;        }    };

(3) Group class

    // Group    var group=Function (name) {        this. name=name;          var students=New  Array ();         var addstudents=function (student) {            students.push (student);             return  This ;        }           this. gestudent=function () {              return  students;          }    };

(4) Student class

 //  student class   var  student=function (name) { this.name=name;  this . Gotoclass=function () {Docume Nt.write (name  + " : I'm a student, I'm going to class.   " )};  this . Finishclass=function () {docu Ment.write (name  + : finally dismissed.   " ); }        };

Second, the test should meet the following:

(1) every a class of 2 groups (one Class one group, one class two groups, the second class one group, the second class two groups)
(2) school computer classrooms are limited, and each group of students is divided into classes
(3) Now we're writing backwards, from students--group----to school
 //Student Examples    varAstudent=NewStudent"I'm a classmate of a ."); varBstudent=NewStudent"I'm a classmate of B ."); varCstudent=NewStudent"I'm a C classmate ."); varDstudent=NewStudent"I'm a Class D ."); varEstudent=NewStudent"I'm a classmate of e ."); varFstudent=NewStudent"I'm a student of F ."); varGstudent=NewStudent"I'm a classmate of G ."); varHstudent=NewStudent"I'm a classmate of H ."); varIstudent=NewStudent"I am a classmate of I");
//Class Example (1)     varclass1=NewClasses"Class One"); //Group 1    varOneone=NewGroup"one class, one group ."); //add students to the grouponeone.addstudents (astudent). addstudents (bstudent); //Group 2    varonetwo=NewGroup"two groups in one class"); //add students to the grouponetwo.addstudents (cstudent). addstudents (dstudent);   Class1.addgroup (Oneone). AddGroup (Onetwo); //Class Example 2    varClass2=NewClasses"Class Two"); //Group 1    varTowone=NewGroup"Class Two, one group ."); //add students to the grouptowone.addstudents (estudent). addstudents (fstudent); //Group 2    vartowtwo=NewGroup"class Two, group Two"); //add students to the grouptowtwo.addstudents (gstudent). Addstudents (hstudent). addstudents (istudent); //Class2.addgroup (Towone). AddGroup (Towtwo);
// School Examples    var usschool=New School (" combined mode school ");

finally , School, we are ready to go to class as required,

Schedule as: Class one group to class school----------Students

 varClasses=usschool.getclass ();//class        for(varI=0; i<classes.length;i++){           if(classes[i].name=="Class One"){                for(varj=0; J<classes[i].getgroups (). length;j++){                    if(Classes[i].classes[i].getgroups () [j]=="a set"){                       varstudents=classes[i].classes[i].getgroups () [J].gestudent ();  for(vark=0; k<students.length;k++) {students[k].gotoclass (); }                    }               }           }       }

finally , I just want to arrange for one to write so much code, that is a school has thousands of classes, then go crazy.

This is certainly not suitable for business expansion, so we use a combination of patterns to solve the above problems.
Why use Design patterns?
Because the design pattern has some of the following ways to do this:
(1) Grouping objects into two categories (grouped objects, and leaf objects)
(2) Combination object and leaf object realization: Same batch operation
(3) Actions performed on a composite object can be passed down to the leaf node
(4) This weakens the coupling between classes and classes
(5) His usual technique is to combine objects into objects of a property structure.
Read the next article: JavaScript combination mode-Improving the example introduced above

JavaScript combination Mode---introduced

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.