views:

138

answers:

2

I have question regarding composite pattern.

Is the base class "Component" act like a pointer to point leaf object in "Composite" class?

Edit: Let me ask my question in following words. "What is the relation between Composite and Component class?"

Here is the uml class diagram of the pattern.

alt text

+1  A: 

Component

  • is the abstraction for all components, including composite ones
  • declares the interface for objects in the composition
  • (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate

Leaf

  • represents leaf objects in the composition
  • implements all Component methods

Composite

  • represents a composite Component (component having children)
  • implements methods to manipulate children
  • implements all Component methods, generally by delegating them to its children

http://en.wikipedia.org/wiki/Composite_pattern

Iain Hoult
My question is regarding the relation between composite and component.As you can see diamond shape symbol with composite class.
Syed Tayyab Ali
+3  A: 

Relation between composite and component:

1) Leaf and Composite usually implement one interface or one abstract class. In your diagram they extend Component. So, the relation on your diagram is inheritance.

2) Composite contains instances of Component. Component, as it occasionally can be Composite, can also contain instances of Component. This is called recursive composition. In general, the relation is called aggregation.

Roman
Indeed, it is aggregation relation.
Syed Tayyab Ali
@Syed: not quite, as I mention there are 2 kind of relations between Component and Composite: inheritance and aggregation. Combining both these relations gives us a structure which is called *recursive composition* in oop theory.
Roman
Yes, you are right.
Syed Tayyab Ali