- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
-
- // Component
- class Graphic {
- public:
- virtual void print() = 0;
- };
-
- // Composite
- class CompositeGraphic : public Graphic {
- public:
- // constructor
- CompositeGraphic(const string &name) {
- mName = name;
- }
- // print
- void print() {
- cout << mName << endl;
- vector<Graphic*>::iterator it;
- for (it = mChildGraphics.begin();
- it != mChildGraphics.end();
- ++it)
- {
- (*it)->print();
- }
- }
- // add
- void add(Graphic *graphic) {
- mChildGraphics.push_back(graphic);
- }
- void remove(Graphic *graphic) {
- vector<Graphic*>::iterator it;
- it = find(mChildGraphics.begin(), mChildGraphics.end(), graphic);
- if (it != mChildGraphics.end()) {
- delete *it;
- mChildGraphics.erase(it);
- }
- }
- private:
- vector<Graphic*> mChildGraphics;
- string mName;
- };
-
- // Leaf
- class Ellipse : public Graphic {
- public:
- // constructor
- Ellipse(const string &name) {
- mName = name;
- }
- // print
- void print() {
- cout << mName << endl;
- }
- string mName;
- };
-
- // Client
- int main() {
- // initialize four ellipses
- Ellipse *ellipse1 = new Ellipse("Ellipse 1");
- Ellipse *ellipse2 = new Ellipse("Ellipse 2");
- Ellipse *ellipse3 = new Ellipse("Ellipse 3");
- Ellipse *ellipse4 = new Ellipse("Ellipse 4");
-
- // initialize three composite graphics
- CompositeGraphic *graphic = new CompositeGraphic("Graphic");
- CompositeGraphic *graphic1 = new CompositeGraphic("Graphic 1");
- CompositeGraphic *graphic2 = new CompositeGraphic("Graphic 2");
-
- // Composes the graphics
- graphic1->add(ellipse1);
- graphic1->add(ellipse2);
- graphic1->add(ellipse3);
-
- graphic2->add(ellipse4);
-
- graphic->add(graphic1);
- graphic->add(graphic2);
-
- // prints the complete graphic
- graphic->print();
-
- return 0;
- }
-
Graphic
Graphic 1
Ellipse 1
Ellipse 2
Ellipse 3
Graphic 2
Ellipse 4
- #include <iostream>
- #include <string>
- #include <list>
- #include <algorithm>
- using namespace std;
-
- // The 'Component' abstract class
- class Component {
- public:
- // constructor
- Component(const string &name) {
- _name = name;
- }
- virtual ~Component() {
- // delete 確認
- //cout << "delete " << _name << endl;
- }
- virtual void Add(Component *c) = 0;
- virtual void Remove(Component *c) = 0;
- virtual void Display(int depth) = 0;
- protected:
- string _name;
- };
-
- // The 'Composite' class
- class Composite : public Component {
- public:
- // constructor
- Composite(const string &name) : Component(name) { }
- // destructor
- ~Composite() {
- list<Component*>::iterator it;
- for (it=_children.begin(); it!=_children.end(); ++it) {
- delete *it;
- }
- }
- // Add
- void Add(Component *component) {
- _children.push_back(component);
- }
- // Remove
- void Remove(Component *component) {
- list<Component*>::iterator it;
- if ((it = find(_children.begin(), _children.end(), component))
- != _children.end())
- {
- delete *it;
- _children.erase(it);
- }
- }
- // Display
- void Display(int depth) {
- string str(depth, '-');
- cout << str << _name << endl;
- // Recursively display child nodes
- list<Component*>::iterator it;
- for (it=_children.begin(); it!=_children.end(); ++it) {
- (*it)->Display(depth + 2);
- }
- }
- private:
- list<Component*> _children;
- };
-
- // The 'Leaf' class
- class Leaf : public Component {
- public:
- // constructor
- Leaf(const string &name) : Component(name) { }
- // Add
- void Add(Component *c) {
- cout << "Cannot add to a leaf" << endl;
- }
- // Remove
- void Remove(Component *c) {
- cout << "Cannot remove from a leaf" << endl;
- }
- // Display
- void Display(int depth) {
- string str(depth, '-');
- cout << str << _name << endl;
- }
- };
-
- // client
- int main() {
- // Create a tree structure
- Composite *root = new Composite("root");
- root->Add(new Leaf("Leaf A"));
- root->Add(new Leaf("Leaf B"));
-
- Composite *comp = new Composite("Composite X");
- comp->Add(new Leaf("Leaf XA"));
- comp->Add(new Leaf("Leaf XB"));
-
- root->Add(comp);
- root->Add(new Leaf("Leaf C"));
-
- // Add and remove a leaf
- Leaf *leaf = new Leaf("Leaf D");
- root->Add(leaf);
- root->Remove(leaf);
-
- // Recursively display tree
- root->Display(1);
-
- delete root;
-
- return 0;
- }
-
-root
---Leaf A
---Leaf B
---Composite X
-----Leaf XA
-----Leaf XB
---Leaf C
- #include <iostream>
- #include <string>
- #include <list>
- #include <algorithm>
- using namespace std;
-
- // The 'Component' Treenode
- class DrawingElement
- {
- public:
- // constructor
- DrawingElement(const string &name) {
- _name = name;
- }
- // destructor
- virtual ~DrawingElement() {
- // delete 確認
- //cout << "deleting " << _name << endl;
- }
- virtual void Add(DrawingElement *d) = 0;
- virtual void Remove(DrawingElement *d) = 0;
- virtual void Display(int indent) = 0;
- protected:
- string _name;
- };
-
- // The 'Leaf' class
- class PrimitiveElement : public DrawingElement {
- public:
- // constructor
- PrimitiveElement(const string &name) : DrawingElement(name) { }
- // Add
- void Add(DrawingElement *d) {
- cout << "Cannot add to a PrimitiveElement" << endl;
- }
- // Remove
- void Remove(DrawingElement *d) {
- cout << "Cannot remove to a PrimitiveElement" << endl;
- }
- // Display
- void Display(int indent) {
- string str(indent, '-');
- cout << str << " " << _name << endl;
- }
- };
-
- // The 'Composite' class
- class CompositeElement : public DrawingElement {
- public:
- // constructor
- CompositeElement(const string &name) : DrawingElement(name) { }
- // destructor
- virtual ~CompositeElement() {
- list<DrawingElement*>::iterator it;
- for (it=elements.begin(); it!=elements.end(); ++it) {
- delete (*it);
- }
- }
- // Add
- void Add(DrawingElement *d) {
- elements.push_back(d);
- }
- void Remove(DrawingElement *d) {
- list<DrawingElement*>::iterator it;
- it = find(elements.begin(), elements.end(), d);
- if (it != elements.end()) {
- delete *it;
- elements.erase(it);
- }
- }
- void Display(int indent) {
- string str(indent, '-');
- cout << str << "+ " << _name << endl;
- list<DrawingElement*>::iterator it;
- for (it=elements.begin(); it!=elements.end(); ++it) {
- (*it)->Display(indent + 2);
- }
- }
- private:
- list<DrawingElement*> elements;
- };
-
- // client
- int main() {
- // Create a tree structure
- CompositeElement *root = new CompositeElement("Picture");
- root->Add(new PrimitiveElement("Red Line"));
- root->Add(new PrimitiveElement("Blue Circle"));
- root->Add(new PrimitiveElement("Green Box"));
-
- // Create a branch
- CompositeElement *comp = new CompositeElement("Two Circles");
- comp->Add(new PrimitiveElement("Black Circle"));
- comp->Add(new PrimitiveElement("White Circle"));
- root->Add(comp);
-
- // Add and remove a PrimitiveElement
- PrimitiveElement *pe = new PrimitiveElement("Yellow Line");
- root->Add(pe);
- root->Remove(pe);
-
- // Recursively display node
- root->Display(1);
-
- delete root;
-
- return 0;
- }
-
-+ Picture
--- Red Line
--- Blue Circle
--- Green Box
---+ Two Circles
----- Black Circle
----- White Circle