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
Componentis 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 baseComponentclass and make it virtual. You can provide an empty implementation inComponent.Your composite implementation is then simply:
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
ComponenttoTreeItem?)Your current
removeComponent()function takes aComponentpointer, thus inferring that anyComponentcan be removed from the tree (includingComposites). This seems correct to me. You might well need to removeComposites. Thus, you can simply compare pointers.However, you then seem to be comparing Id’s (via an assumed equality overload) that only
Leafshave.If you want to provide an additional function to remove by Id, then I would move the GetID() function to the base
Componentclass too, and compare against that.Compositeobjects can return -1 or some other null marker.eg.