Talk:Composite pattern

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Java (Rated Start-class, Low-importance)
WikiProject icon This article is within the scope of WikiProject Java, a collaborative effort to improve the coverage of Java on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the project's quality scale.
 Low  This article has been rated as Low-importance on the project's importance scale.
 
Note icon
This article has been automatically rated by a bot or other tool because one or more other projects use this class. Please ensure the assessment is correct before removing the |auto= parameter.
WikiProject Computer science (Rated Start-class)
WikiProject icon This article is within the scope of WikiProject Computer science, a collaborative effort to improve the coverage of Computer science related articles on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the project's quality scale.
 ???  This article has not yet received a rating on the project's importance scale.
 

comment[edit]

This has to be one of the most unprofessional UML diagrams I've seen for one of the most common patterns. As per the user above's complaint about the PERL, Java would be a better language, but in interest of neutrality I'd suggest doing the whole thing in a UML sequence diagram to maintain language neutrality. And can't someone with the GoF CD just quote and paste portions outlining the GoF example -- it's much better.

I added two links to discussions on the pattern. What is needed is a realistic example. For instance an example where the pattern is not used and then how refactoring it leads to the pattern. I have found several moderetaly convincing examples on the web but no really convincing one. Concerning language neutrality: UML sequence language is no more neutral than C++ or Java. MikalZiane 12:44, 26 June 2006 (UTC)

I believe the quality of this article is the code examples as there is nowhere else on the web that both explains the theory and show concrete implementations. A encyclopedia should contain an explanation and history and/or a picture and/or video sequence and/or map and/or examples and/or links to other resources of a given topic. This article only does half and should therefore not be shorten in anyway that will limit the way it elaborate on the topic "Composite pattern". Someone should update (or add) a C# 2.0 example with generics. Also an example in Smalltalk should be added for completeness (a C# 2.0 implementation with the [C5] library and a Java implementation with the Java Collections Framework could also be added). --DotnetCarpenter 09:42, 1 November 2006 (UTC)

I think the article would be a lot better if the code examples were consistent with the UML diagram and its explanation. The abstract Component class is stated to "declare the interface for objects in the composition" and "implement default behavior for the interface common to all classes, as appropriate". Neither of these can be seen implemented in the example code. Should the examples be changed to follow the diagram? Should the benefits and drawbacks of having a more complete vs. simple interface in Component class (cf. GoF book items 3 and 4 on pages 167-169) be mentioned, too? --Uffis (talk) 10:51, 12 June 2008 (UTC)


hi, Uffis: what you were talking about was an abstract class with some methods with default functional behavior implemented. but you cannot really implement in the interface itself. All the abstract methods defined inside the interface were meant to be overriden or implemented according to the implemented classes themselves. this is my personal opinion. I'm open for discussion.

Composite c++ example[edit]

I changed the c++ example the old one was awful: horribly indented, compiled with many warnings, segfaulted, obfuscated, etc. This one I believe is much better and doesn't have any warnings under: g++ -o test test.cpp -Wall -pedantic -ansi. Let me know what you think--Michael miceli (talk) 02:43, 30 March 2008 (UTC)

Bad class design[edit]

In UML diagram, the Component should not have methods to manage children components (add, remove and getChild) because the Leaf class which inherit from the Component class should not have child components. Those methods should not exists in the Leaf class. When the component tree is built, the add and remove methods are called using a Composite class instance instead of a Component interface reference.

--DavidL (talk) 13:48, 9 May 2009 (UTC)

Agreed. I was just going to post something saying the same thing.
Mmurfin (talk) 19:35, 31 December 2009 (UTC)

Apparently, this was actually the way the pattern was defined by the GoF; see this extensive discussion. Pretty much everyone agrees that it was defined incorrectly, but there's nothing we can do about it now. Perhaps the best solution would be to uniformly describe the version without the extra interface methods in the article, but add a section about this "variation". I'm uploading a new version of the UML diagram without the superfluous methods. « Aaron Rotenberg « Talk « 00:21, 10 February 2010 (UTC)

by inheriting, you meant implmenting, right? 'cauz either composite or leaf is supposed to implement the common method from the interface, not from the superclass/baseclass. unless you are trying to define an abstract class instead of an interface, if you are sure there are some common functionality to be implemented in the abstract class "Component". otherwise it's better to declare Component an interface, if a relationship of polymorphism is expected.


Open Discussion: Composite Vs. Aggregation[edit]

Some may argue that the diagram above demonstrates a relationship of aggregation, not yet of composite, as the dependent relationship between leafs and composite is missing from the diagram. that is to say, Composite and Aggregation differ at that the leafs should have the same lifetime as its composite in a relationship of composite. In other words, the leaf lives and dies as the composite lives and dies. Especially when the composite got destroyed, everything in the composite should be destroyed as well.

When only the references (class, reference typed, objects themselves stored random on heap when references stored on stack, first in first out, requiring an overhead of memory manager of CLR and garbage collector to dispose the orphaned objects without a valid reference, out of our reach, totally depends on CLR who only concerns memory, not resources) are added to the composite, the pointing objects will not be destroyed when the composite is destroyed;

however, if the objects themselves (structs, value typed, stored in order on stack, first in last out) are added to the composite, they will be destroyed as the composited is destroyed


— Preceding unsigned comment added by Firetinder (talkcontribs) 14:01, 30 September 2011 (UTC) 

if the example is based entirely on GoF - they do not use an interface, but an abstract class. — Preceding unsigned comment added by 188.28.67.227 (talk) 09:49, 31 May 2012 (UTC)