Removing an element from the composite structure of a pattern

Suppose we have the simplest structure, classes Component, Leaf : Componentand Composite : Component. Each class object Leafhas int idone that gives it its identifier. A complex class will look like this:

class Composite : public Component
{
public:
    void removeComponent(Component*);
// other stuff
private:
    std::vector<Component*> coll;
};

And the sth like sheet class:

class Leaf : public Component
{
public:
    //stuff
    int getID();
private:
    int id;
};

And the question is how to define a function removeComponent(Component* cmp). cmp is actually a Leaf, but I need to access the Componentvector coll, so it should be Component(I think). The removeComponent method takes an object Leafand removes all other Sheets with the same identifier from the entire structure.

Now I thought of two paths (none of which work: 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..
        }

    }

, . , , , , Leaf, . , ?:)

+5
2

, , Component. - -, , node? node, , .

, removeComponent() Component . Component.

:

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);
    }
}

.

, , : Component id Components. (, , Component TreeItem?)

removeComponent() Component, , Component ( Composites). . Composites. , .

, , , Id ( ), Leafs.

, GetID() Component . Composite -1 - .

.

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);
    }
}
+3

:

void Composide::removeComponent(Component* cmp)
{
    std::vector<Component*>::const_iterator it;
    for(it = coll.begin(); it != coll.end(); ++it)
    {
        if((*it)->equals(*cmp))
        {
            delete *it;
            coll.erase(it);
            return;//can there be more than one?
        }
        else
        {
            //iterative call
            it->removeComponent(*cmp);
        }
    }
}

void Composide::~Composite()
{
    std::vector<Component*>::const_iterator it;
    for(it = coll.begin(); it != coll.end(); ++it)
    {
        delete *it;
        it = coll.erase(it);
    }
}

Composite , Composite .

, . .

equals (..) .

: if/else

-1

All Articles