Skip to Content.
Sympa Menu

gang-of-4-patterns - RE: [gang-of-4-patterns] Re: <Composite pattern with type checking>

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

RE: [gang-of-4-patterns] Re: <Composite pattern with type checking>


Chronological Thread 
  • From: Jesús Alonso <kenchoweb AT hotmail.com>
  • To: ttn13w30a9 AT mx1.ttcn.ne.jp, gang-of-4-patterns AT cs.uiuc.edu
  • Cc:
  • Subject: RE: [gang-of-4-patterns] Re: <Composite pattern with type checking>
  • Date: Tue, 28 Feb 2006 01:08:50 +0100
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Ohayô!

I think this is mostly a structural problem. I might be wrong, but I think it's a problem of name collision.
Leaf inherits both from NotComposite and Component. As NotComposite is a Component as well, it's inheriting both methods at the same level.
When you call Add and Remove through a pointer to Component, you're calling the Component implementation of the methods stored in Leaf (lf)
But when you call Add from lfp, the compiler doesn't know if it should use the implementation provided by Component or NotComposite, as both are inherited directly from each of its parents.

I guess the solution is to declare Leaf just as a NotComposite (and indirectly derived from Component). Just using the following declaration:
class Leaf:public NotComposite{
};

Hope it helps (and hope it works as well! :S)

Jaa mata! ^__^
Jesús Alonso Abad


Hello.
About the Compsite Pattern, I thought of the implementation with type checking.
A code is as follows.
In main, A leaf object is going to call an Add method, and an error occurs.


class Component {
public:
virtual void Add(Component* parent);
virtual void Remove(Component* parent);
};
void Component::Add(Component* parent){};
void Component::Remove(Component* parent){};

class Composite:public Component{
};

class NotComposite{
virtual void Add(Component* parent);
virtual void Remove(Component* parent);
};
void NotComposite::Add(Component* parent){};
void NotComposite::Remove(Component* parent){};

class Leaf:public Component, public NotComposite{
};


int main(){
Component* cmp;
Leaf* lfp;
Leaf lf;
Composite cs;

cmp = &lf;
cmp->Add(cmp);
cmp->Remove(cmp);

lfp = &lf;

lfp->Add(cmp); //compile error
lf.Add(cmp); //compile error

return 0;
}

Please tell an opinion.
Thank you.

Atsushi Tanabe(Japanese)


_______________________________________________
gang-of-4-patterns mailing list
gang-of-4-patterns AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns






Archive powered by MHonArc 2.6.16.

Top of Page