Composition mode, which combines objects into attribute structure to represent the "part whole" hierarchy. The combination mode makes the use of single object and combination object consistent. Its UML diagram is as follows:
The main solution: in our tree structure problem, it blurs the concept of simple elements and complex elements. The client program can process complex elements as simple elements, so that the internal structure of the client program and complex elements can be decoupled.
How to solve this problem: the tree branch and the leaf implement a unified interface, and the tree branch combines the interface internally.
Key code: the interface is composed within the tree branch, and contains the internal attribute list, in which components are placed.
The relationship between colleges in a university can be expressed in a combination mode. For example, the engineering college includes the mechanical college and the computer college. The code is as follows:
C++ Code:
1 #include <iostream> 2 #include <vector> 3 #include <list> 4 5 using namespace std; 6 7 class College{ 8 protected: 9 string name; 10 public: 11 College(string _name){ 12 name = _name; 13 } 14 virtual void add(College* p) = 0; 15 virtual void remove(string name) = 0; 16 virtual void display() = 0; 17 string getName(){ 18 return this->name; 19 } 20 }; 21 22 class ConcreteCollege: public College{ 23 public: 24 ConcreteCollege(string _name): College(_name){} 25 void add(College* p){ 26 shared_ptr<College> temp(p); 27 listCollege.push_back(temp); 28 } 29 void remove(string name){ 30 auto it = listCollege.begin(); 31 for(it; it != listCollege.end(); it++){ 32 shared_ptr<College> temp(*it); 33 string strName = temp->getName(); 34 if(name == strName){ 35 listCollege.erase(it); 36 } 37 } 38 } 39 void display(){ 40 cout << this->getName()<< endl; 41 for (auto it = listCollege.begin(); it != listCollege.end(); it++){ 42 (*it)->display(); 43 } 44 } 45 private: 46 vector<shared_ptr<College>> listCollege; 47 }; 48 49 int main() 50 { 51 College *c1 = new ConcreteCollege("Engineering College"); 52 c1->add(new ConcreteCollege("Mechanical College")); 53 c1->add(new ConcreteCollege("Computer College")); 54 c1->display(); 55 return 0; 56 }
Here we use the shared PTR smart pointer to avoid the trouble caused by delete.
Java code
1 public interface ICollege { 2 public void add(ICollege iCollege); 3 public void delete(ICollege iCollege); 4 public void display(); 5 } 6 7 public class College implements ICollege { 8 9 private String name; 10 11 private ArrayList<ICollege> listCollege; 12 13 public String getName(){return this.name;} 14 15 public College(String name){ 16 listCollege = new ArrayList<ICollege>(); 17 this.name = name; 18 } 19 20 public void add(ICollege iCollege) { 21 listCollege.add(iCollege); 22 } 23 24 public void delete(ICollege iCollege) { 25 listCollege.remove(iCollege); 26 } 27 28 public void display() { 29 System.out.println(name); 30 for (ICollege iCollege: 31 listCollege) { 32 iCollege.display(); 33 } 34 } 35 } 36 37 public class Main { 38 public static void main(String[] args) { 39 ICollege c = new College("Engineering College"); 40 c.add(new College("Mechanical College")); 41 c.add(new College("Computer College")); 42 c.display(); 43 } 44 }