#include <iostream>
#include <string>
#include <list>
using namespace std;
class Component
{
public:
Component(string name):name(name){}
virtual void Add(Component* c) = 0;
virtual void Remove(Component* c) = 0;
virtual void Display(int depth) = 0;
protected:
string name;
};
//叶子
class Leaf : public Component
{
public:
Leaf(string name):Component(name){}
virtual void Add( Component* c )
{
//throw std::exception("The method or operation is not implemented.");
cout << "Can not add to a Leaf....\n";
}
virtual void Remove( Component* c )
{
cout << "Can not remove from a Leaf...\n";
}
virtual void Display( int depth )
{
string str(depth, '-');
cout << str << name << endl;
}
};
//组合
class Composite : public Component
{
public:
Composite(string name):Component(name)
{
}
virtual void Add( Component* c )
{
children.push_back(c);
}
virtual void Remove( Component* c )
{
children.remove(c);
}
virtual void Display( int depth )
{
string str(depth,'-');
cout << str << name << endl;
for (list<Component*>::iterator i = children.begin(); i != children.end(); ++i)
{
(*i)->Display(depth + 2);
}
}
private:
list<Component*> children;
};
int main(void)
{
Composite *root = new Composite("root");
root->Add(new Leaf("LeafA"));
root->Add(new Leaf("LeafB"));
Composite *comp = new Composite("Composite x");
comp->Add(new Leaf("LeafXA"));
comp->Add(new Leaf("LeafXB"));
root->Add(comp);
comp = new Composite("Composite Y");
comp->Add(new Leaf("LeafYA"));
comp->Add(new Leaf("LeafYB"));
Component *comp1 = new Composite("Composite Z");
comp1->Add(new Leaf("LeafZA"));
comp1->Add(new Leaf("LeafZB"));
comp->Add(comp1);
root->Add(comp);
root->Display(1);
return 0;
}
#include <string>
#include <list>
using namespace std;
class Component
{
public:
Component(string name):name(name){}
virtual void Add(Component* c) = 0;
virtual void Remove(Component* c) = 0;
virtual void Display(int depth) = 0;
protected:
string name;
};
//叶子
class Leaf : public Component
{
public:
Leaf(string name):Component(name){}
virtual void Add( Component* c )
{
//throw std::exception("The method or operation is not implemented.");
cout << "Can not add to a Leaf....\n";
}
virtual void Remove( Component* c )
{
cout << "Can not remove from a Leaf...\n";
}
virtual void Display( int depth )
{
string str(depth, '-');
cout << str << name << endl;
}
};
//组合
class Composite : public Component
{
public:
Composite(string name):Component(name)
{
}
virtual void Add( Component* c )
{
children.push_back(c);
}
virtual void Remove( Component* c )
{
children.remove(c);
}
virtual void Display( int depth )
{
string str(depth,'-');
cout << str << name << endl;
for (list<Component*>::iterator i = children.begin(); i != children.end(); ++i)
{
(*i)->Display(depth + 2);
}
}
private:
list<Component*> children;
};
int main(void)
{
Composite *root = new Composite("root");
root->Add(new Leaf("LeafA"));
root->Add(new Leaf("LeafB"));
Composite *comp = new Composite("Composite x");
comp->Add(new Leaf("LeafXA"));
comp->Add(new Leaf("LeafXB"));
root->Add(comp);
comp = new Composite("Composite Y");
comp->Add(new Leaf("LeafYA"));
comp->Add(new Leaf("LeafYB"));
Component *comp1 = new Composite("Composite Z");
comp1->Add(new Leaf("LeafZA"));
comp1->Add(new Leaf("LeafZB"));
comp->Add(comp1);
root->Add(comp);
root->Display(1);
return 0;
}