-1

I'm studying composite design pattern. I just wanted to ask that can a leaf in a composite pattern inherit from any other class than component?

enter image description here

dbugger
  • 15,868
  • 9
  • 31
  • 33
Ramzah Rehman
  • 184
  • 2
  • 14
  • Is there somebody to answer? – Ramzah Rehman Dec 03 '16 at 05:29
  • Why couldn't it? In most OO languages, you **always** inherit from a base Object class anyway. And even then, there is no design pattern police that will send you in jail if you adapt the pattern to your needs. – JB Nizet Dec 03 '16 at 12:47
  • The arrow from "Team" to "Participant" in your diagram should be a Aggregation arrow (diamond) pointing to Team, because "One Team has multiple Participants". – Philipp Dec 03 '16 at 13:04

2 Answers2

0

In the Composite pattern, Composite and a Leaf implement the same interface Component. That's the point of the pattern.

The purpose is that you can write code which treats a Leaf or a Composite consisting of multiple Leafs (or multiple sub-Composites) the same way.

When you are using an object-oriented language which supports interfaces (or is able to simulate them, for example with multiple inheritance of abstract classes), it is up to the classes Leaf and Composite how they implement the Component interface. They can do so by inheriting from a class which already implements Component, or they can implement it on their own.

Philipp
  • 67,764
  • 9
  • 118
  • 153
0

Yes, it is possible, Please refer to This from coding helmet, its a really good sample that implements some design patterns and composition is the glue that lets leaf classes encapsulate and isolate behavior from other classes.
I hope this sample help to get my idea.
Regards!