Talk:Composite pattern

Latest comment: 6 years ago by Faught in topic Describing the variants

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)Reply

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)Reply

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)Reply


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)Reply

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)Reply

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

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)Reply

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)Reply 

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)Reply

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.

This message was posted before February 2018. After February 2018, "External links modified" talk page sections are no longer generated or monitored by InternetArchiveBot. No special action is required regarding these talk page notices, other than regular verification using the archive tool instructions below. Editors have permission to delete these "External links modified" talk page sections if they want to de-clutter talk pages, but see the RfC before doing mass systematic removals. This message is updated dynamically through the template {{source check}} (last update: 5 June 2024).

  • 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.

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

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)Reply

References

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

Describing the variants

edit

In the Structure section, two variants are described with the implication that neither is more prominent or desirable. But later it says "The Composite design pattern emphasizes uniformity over type safety," giving another diagram very similar to the "design for type safety variant." How can we clean this up?

One more possibility for improvement, it's not clear what the context is for the code under "Simple example". It seems to be a continuation of the C# example, maybe in the same source file.

Faught (talk) 14:31, 26 November 2018 (UTC)Reply