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.


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 (talk) 09:49, 31 May 2012 (UTC)

External links modified[edit]

Hello fellow Wikipedians,

I have just modified one external link on Composite pattern. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:

When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.

You may set the |checked=, on this template, to true or failed to let other editors know you reviewed the change. If you find any errors, please use the tools below to fix them or call an editor by setting |needhelp= to your help request.

  • If you have discovered URLs which were erroneously considered dead by the bot, you can report them with this tool.
  • If you found an error with any archives or the URLs themselves, you can fix them with this tool.

If you are unable to use these tools, you may set |needhelp=<your help request> on this template to request help from an experienced user. Please include details about your problem, to help other editors.

Cheers.—InternetArchiveBot (Report bug) 19:31, 11 August 2017 (UTC)

UML class and object diagram for the Composite design pattern.[edit]

I have shared the following UML diagrams I have published on Design Patterns Open Online Learning. Your comments are welcomed.

A sample UML class and object diagram for the Composite design pattern. [1]

In the above UML class diagram, the Client class doesn't refer to the Leaf and Composite classes directly (separately). Instead, the Client refers to the common Component interface for performing an operation (operation()).
The Leaf class has no children and implements an operation directly.
The Composite class maintains a container of child components (children) and implements an operation by forwarding it to these children (for each child in children: child.operation()). Additionally, Composite implements the child-management operations (add(child)).

The object collaboration diagram shows the run-time interactions: In this sample scenario, a Client object sends a request to the top-level Composite object (of type Component) in the hierarchy (tree structure). The request is forwarded to all child components (Leaf and Composite objects of type Component) recursively, that is, the request is performed on all objects downwards the tree structure.

Vanderjoe (talk) 23:36, 31 August 2017 (UTC)

  1. ^ "The Composite design pattern - Structure and Collaboration". Retrieved 2017-08-12.