Suppose we have the simplest structure, classes Component
, Leaf : Component
and Composite : Component
. Each object of Leaf
class has an int id
which gives it its identity. The composite class would have sth like:
class Composite : public Component
{
public:
void removeComponent(Component*);
// other stuff
private:
std::vector<Component*> coll;
};
And leaf class sth like:
class Leaf : public Component
{
public:
//stuff
int getID();
private:
int id;
};
And the question is how to define the function removeComponent(Component* cmp)
. cmp is actually a Leaf
, but I need to access the Component
vector coll, so it needs to be a Component
(I think). The removeComponent method takes a Leaf
object, and removes all other Leaves with the same ID, from the whole structure.
Now, I have thought of two ways (neither of which works :P):
First
void Composide::removeComponent(Component* cmp)
{
std::vector<Component*>::const_iterator it;
for(it = coll.begin(); it != coll.end(); ++it)
{
(*it)->removeComponent(cmp);
// where removeComponent is defined as virtual in Component, but
// the problem is that Leaf cannot erase itself from the collection
}
}
Second
void Composide::removeComponent(Component* cmp)
{
std::vector<Component*>::const_iterator it;
for(it = coll.begin(); it != coll.end(); ++it)
{
if((*it)->equals(*cmp))
it = erase(it);
// But here I can't define equals function for Composite elements,
// so then I'd have to make functions which return the type of Component,
// and in case of Composite call recursively the function and
// in the case of Leaf call the equals() function and erase if needed.
// This however looks like really bad OOP practice, and I'd like to avoid
// using those methods which return type..
}
}
There has to be a neat, nice way to do this. I kind of think that the method should look something like the upper-mentioned 1st way, only I really don't know how to enable the Leaf
to delete itself from the vector. Help me please? :)
You seem to be getting confused as to what Component
is exactly. Is it some business object, or is it a class representing a tree node? If it's a tree node, then all tree nodes should support the same operations to allow easy recursion.
As such I would move the definition of removeComponent()
to the base Component
class and make it virtual. You can provide an empty implementation in Component
.
Your composite implementation is then simply:
void Composide::removeComponent(Component* cmp)
{
std::vector<Component*>::const_iterator it;
for(it = coll.begin(); it != coll.end(); ++it)
{
if((*it)->equals(*cmp))
it = erase(it);
else
(*it)->removeComponent(cmp);
}
}
EDIT: Regarding Id's
Again, I think you might be muddling two concepts, Component id's, and Components.
(Perhaps it would be better if you renamed Component
to TreeItem
?)
Your current removeComponent()
function takes a Component
pointer, thus inferring that any Component
can be removed from the tree (including Composites
). This seems correct to me. You might well need to remove Composites
. Thus, you can simply compare pointers.
However, you then seem to be comparing Id's (via an assumed equality overload) that only Leafs
have.
If you want to provide an additional function to remove by Id, then I would move the GetID() function to the base Component
class too, and compare against that. Composite
objects can return -1 or some other null marker.
eg.
void Composite::getID()
{
return -1;
}
void Composide::removeComponent(Component* cmp)
{
std::vector<Component*>::const_iterator it;
for(it = coll.begin(); it != coll.end(); ++it)
{
if((*it) == cmp)
it = erase(it);
else
(*it)->removeComponent(cmp);
}
}
void Composite::removeComponentById(int id)
{
std::vector<Component*>::const_iterator it;
for(it = coll.begin(); it != coll.end(); ++it)
{
if((*it)->getID() == id)
it = erase(it);
else
(*it)->removeComponentById(id);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With