Skip to Content.
Sympa Menu

gang-of-4-patterns - Re: [gang-of-4-patterns] Doubt on Composite pattern (GOF)

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

Subject: Design Patterns discussion

List archive

Re: [gang-of-4-patterns] Doubt on Composite pattern (GOF)


Chronological Thread 
  • From: "Bangels Stefan" <fader AT rootshell.be>
  • To: <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: Re: [gang-of-4-patterns] Doubt on Composite pattern (GOF)
  • Date: Sat, 15 Nov 2003 21:40:58 +0100
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

I think you are mixing instances and classes together.

I guess you thought that the UML diagrams represent the objects (instances)
of an example application, this is where you are wrong! The UML diagrams
(classdiagrams) represent classes, not instances!

If you look at the example of the "picture composite" in the gof book, you
only see the classes, not the instances... When writing an application, you
write these classes, and whenever you need a new line, text, rectangle or
picture, you create an instance of the class needed, and work with the
instances.

When you create an instance, then you're application will allocate the
memory needed for the object, and apply the framework of methods and
attributes, as described in the class to your instance. The instance is your
actual object, not the class itself!

An example of multiple composites would be:

In Microsoft word, you can use the GROUP command to compose lines,
rectangles, ... to a group (just like in gof, the group is a
picture-composite)

Word also has the ability to create textboxes, that you also can use as a
container for drawing objects. This is your second composite!

So... Assuming that microsoft has used the composite pattern for doing this,
this is an example of for the use of two different composite CLASSES, not
instances.


If you like an example of multiple INSTANCES of one class, try this:

Create two rectangles... Each rectangle you draw is in fact an instance of
one single class, the rectangle class.


I hope this solved your problem.

Another thing you could have meant, is this:

You create two instances of the picture class, picture1 and picture2.
You add picture1 to picture2, using the add-method of picture2.
You add picture2 to picture1, using the add-method of picture1.
You call the draw-operation of one of the instances.

when doing this, you indeed get into a loop, but normally you limit your
application in doing this... this is not a fault of the pattern, but a fault
of your application!

Kind regards,

Stefan Bangels

----- Original Message -----
From:
<rajesh AT cs.iitm.ernet.in>
To:
<gang-of-4-patterns AT cs.uiuc.edu>
Sent: Saturday, November 15, 2003 5:47 PM
Subject: [gang-of-4-patterns] Doubt on Composite pattern (GOF)


> Hello,
>
> I have a doubt on Composite pattern. Does a composite pattern have more
> than one composite class ? If not, why? If so, what are the applications
> that it contains ?
>
> I feel that if we have two composite classes we will get into infinite
> loop because one composite class will make a call to all the subclasses(of
> component)operation and as it makes a call to other composite class
> (one of the subclass of component) operation method it in turn make calls
> to all other subclasses operation and so on. Is it correct ?
> Looking forward for ur reply
>
> rajesh
>
> --
> ===================================================================
>
>
> "Genius is one percent inspiration and 99 percent perspiration."
> -- Thomas Alva Edison
>
>
> ===================================================================
>
>
> _______________________________________________
> gang-of-4-patterns mailing list
> gang-of-4-patterns AT cs.uiuc.edu
> http://mail.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns
>





Archive powered by MHonArc 2.6.16.

Top of Page