Composite Design Pattern Tutorial

Composite Design Pattern TutorialWelcome to my Composite Design Pattern Tutorial! The Composite design pattern is used to structure data into its individual parts as well as represent the inner workings of every part of a larger object.

The composite pattern also allows you to treat both groups of parts in the same way as you treat the parts polymorphically. This allows your objects to maximize complexity while also remaining dynamic.

All of the code follows the video to help you learn. It is heavily commented.

If you think others would benefit from this video, it helps to tell Google with a click [googleplusone]

Share if you no others may like this [nttfblike]

Code from the Video

SONGCOMPONENT.JAVA

SONGGROUP.JAVA

SONG.JAVA

DISCJOCKEY.JAVA

SONGLISTGENERATOR.JAVA

25 thoughts on “Composite Design Pattern Tutorial”

  1. line 29 of SongListGenerator: industrialMusic.add(dubstepMusic);
    should this read everySong.add(dubstepMusic);?

    these are awesome – i dont know what i’d do without your videos

    1. Thank you πŸ™‚ No, industrialMusic.add(dubstepMusic) is correct. I may have confused you because I considered DubStep to be a version of industrial. That may not be true. I don’t know because I’m definitely not a music expert

  2. Hi Derek

    And thanks for the great tutorials

    I have a Q: How come a β€œthrows” statement, is not required from methods of the abstract class SongComponent ?

  3. Dude, I’m from Chile. You’re helping the entire world with your vids. How awesome is that?
    Take care bro and thanks

  4. Great work! Thank you!

    I know it can be many leaf classes, as long as they implement the same interface. What is not clear to me is if can it be many composite classes. It’s ok to have different ways of grouping composite/leaf objects? Can you think of an example? (I can’t πŸ˜› )

  5. Hi Derek,

    Really nice tutorial. This explains the Pattern and code so well. Thanks a lot!

    Thanks & Regards,
    Varun

  6. Hey Derek, first of all, thank you so much.
    But, i’m a little bit confused with this pattern.
    Can you create a UML Class Diagram with this example and with an UML Object Diagram? Cause I don’t know how to represents “Heavey Metal” “Industrial” and “Dubstep” in the diagram.
    Thanks one more time!

    πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *