标签:bsp osi ring 变化 width 关系 namespace style 课程
概述
示例
1 #include <iostream> 2 #include <list> 3 #include <string> 4 #include <algorithm> 5 6 using namespace std; 7 8 class Component 9 { 10 public: 11 virtual void process() = 0; 12 virtual ~Component(){} 13 }; 14 15 //树节点 16 class Composite : public Component{ 17 18 string name; 19 list<Component*> elements; 20 public: 21 Composite(const string & s) : name(s) {} 22 23 void add(Component* element) { 24 elements.push_back(element); 25 } 26 void remove(Component* element){ 27 elements.remove(element); 28 } 29 30 void process(){ 31 32 //1. process current node 33 34 //2. process leaf nodes 35 for (auto &e : elements) 36 e->process(); //多态调用 37 38 } 39 }; 40 41 //叶子节点 42 class Leaf : public Component{ 43 string name; 44 public: 45 Leaf(string s) : name(s) {} 46 47 void process(){ 48 //process current node 49 } 50 }; 51 52 void Invoke(Component & c){ 53 //... 54 c.process(); 55 //... 56 } 57 58 int main() 59 { 60 61 Composite root("root"); 62 Composite treeNode1("treeNode1"); 63 Composite treeNode2("treeNode2"); 64 Composite treeNode3("treeNode3"); 65 Composite treeNode4("treeNode4"); 66 Leaf leat1("left1"); 67 Leaf leat2("left2"); 68 69 root.add(&treeNode1); 70 treeNode1.add(&treeNode2); 71 treeNode2.add(&leaf1); 72 73 root.add(&treeNode3); 74 treeNode3.add(&treeNode4); 75 treeNode4.add(&leaf2); 76 77 process(root); 78 process(leaf2); 79 process(treeNode3); 80 81 }
总结
标签:bsp osi ring 变化 width 关系 namespace style 课程
原文地址:https://www.cnblogs.com/cxc1357/p/12320329.html