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: "Atsushi Tanabe" <ttn13w30a9 AT mx1.ttcn.ne.jp>
  • To: <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: Re: [gang-of-4-patterns] Re: <Composite pattern with type checking>
  • Date: Wed, 1 Mar 2006 00:55:13 +0900
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Thank you very much for reply.

> I think this is mostly a structural problem. I might be wrong, but I think
> it's a problem of name collision.


That's right.
The compilation error in sample code was intentionally raised.
My attempt prevents that a leaf calls Add method, at compile-time.
But it must be finally checked at run-time.

Thank you.



----- Original Message -----
From: "Jesús Alonso"
<kenchoweb AT hotmail.com>
To:
<ttn13w30a9 AT mx1.ttcn.ne.jp>;

<gang-of-4-patterns AT cs.uiuc.edu>
Sent: Tuesday, February 28, 2006 9:08 AM
Subject: RE: [gang-of-4-patterns] Re: <Composite pattern with type checking>


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