Been watching your videos since I was a student, and years later as an engineer they're still relevant and excellent resource material. Thank you for all the work you've done, the community is forever grateful
Thank you :) I'm mainly teaching topics that I guess are so complex that very few people are interested in them. I'm extremely happy with my community. So many people that I meet online are great friends that I wish I could meet in the real world. It is very gratifying for me to be able to help so many nice people!
From your tutorials on python and java to developing candy crush as an intern and later when I'll manage good with my tasks I'm going to get a junior software engineer position. I just want to say thank you!
I really love your tutorials, really helps me out with my Desin Pattern exams. Sooo much better than all the useless PowerPoints my school is giving me. Keep up the good work!!
If you aren't a CS instructor, you should be! I learn more from a few hours watching your videos than my professors can teach in weeks! Your explanations are top notch, keep up the great work.
Dude, you're maybe never gonna see this, but thanks a lot for all your design patterns video, I was struggling to understand in college, and my finals are near, this is a big help, much appreciation.
You guys determine what I do for the most part. I created Asteroids in parts 50 thru 60 in my Java video tutorial. It covers so many things like collision detection, a lot of physics based stuff like lifelike shooting, flying, etc. Have you seen that? When I start making games I'll start with a simple platformer and move up from there. What type of game do you want to see?
Yes Java, C++ and C# are OOP languages. It won't take that long to learn if you do a little every day and make fun projects. I've known people to learn just about everything in 2 months. They still have to use copy and paste code all of the time, but they know how to use Java pretty well
Another great video! Your videos have been a massive help to me over the past few months so I've donated a few quid via your website just to show my appreciation.
Yes I've seen it. I'll eventually cover frameworks and social networks when I get back into PHP. I know I need to improve on that tutorial. Thanks for the request
This version of the pattern is NOT CORRECT. Every method for all classes is crammed into one abstract class where the default implementation is to throw exceptions. This is a very obvious violation of the Interface Segregation Principle, and a good demonstration of an ANTI-PATTERN. Just define methods where they are supposed to go. SongGroup should have add() and remove(). Song should have getSongName() and getBandName(). An interface or abstract class is needed for the pattern to work, but it should only have the methods that are common to composite objects (SongGroups) and leaf objects (Songs). In this case, that would be one method: displaySongInfo(). Other sources on UA-cam and the internet will demonstrate the pattern correctly.
+Andrew Palmer It is clearly stated in the Design Patterns book that implementing the child management operations in the component instead of in the composite (like Derek did here) is favoring transparency over safety. By default you have the child management operations fail, so that you can catch exceptions when they are called on leaves, (usually indicates a bug anyway), and you override them in the component to actually do what they should do. That way they both composite and leaves appear to have the same interface to the client. If you want to go for safety, sure, do what you said, but don't say that this implementation is actually an ANTI-PATTERN because it's really one of the default ways of implementing this pattern. For more info, see www.google.be/imgres?imgurl=t0.gstatic.com/images%3Fq%3Dtbn:ANd9GcQYYgj_qlNHWQecLsaHLSQxzb4Cgkh1JjsJAAGlFyh1gtkVnK0J&imgrefurl=books.google.com/books/about/Design_Patterns.html?id%3D6oHuKQe3TjQC%26source%3Dkp_cover&h=800&w=600&tbnid=uGBuMT7EpSNMtM:&tbnh=160&tbnw=119&docid=I8O6u1SkhQlJ1M&itg=1&usg=__XhfV5M9JAwk1Efwq-JH04_BGshY=
+Steve Peros I've heard this argument before, but this so-called "transparency" is not a virtue. "Transparency," in this context, means the same thing as "breaking the type system to no benefit." Clients should not look at two objects the same way if they do not support the same operations. Run-time exception handling is not preferable to compile-time checking, which is why I call this implementation an anti-pattern. If you use the pattern correctly (i.e. the SOLID version of the pattern), your code will just fail to compile if you try to call an invalid method, and run-time exception handling is unnecessary. This is one of the key advantages of using languages with static type systems. There is no reason not to let the type system do its job. If you feel like Java's type system just gets in your way, you can go look for a video on Python or Ruby (great languages)!
+Andrew Palmer According to you, using this pattern correctly is only implementing the child management operations in the composite then? And you consider type casting to be a better alternative then when a client is working with a component object and wants to access child management methods? I'm familiar with other languages, but considering this video is a JAVA implementation, I keep my point restricted to JAVA.
I was little bit confused when i saw other implementations of this pattern, but with your implementation about the GROUPs and SONGs it was so easy to understand!! Thank you so much :)
You really deserve a lot more views and subscribers! Most design patterns are very hard for me to understand - not how they are created, but for what kind of problems they can be used - and you are explaining these things really easily using simple examples. Very nice! Please go on with that great work! Suscribed.
PHP isn't an oop language. What I'm covering now is about as complicated as programming gets. C is a low level language that is pretty easy once you understand memory management. C++ is easy after you understand oop. PHP is just a toy in comparison. Start to learn Java and OOD and you'll quickly see the difference
I think modeling files and directories as a composite pattern example of might be more clear than songs and song groups. Also, should use generics to eliminate casting.
Duude... BREATH! :D I'd watched a couple of your tutorials the last days. Explanation is good but the emphasizing... It feels like listening to an auctioneer :D
SongComponent songList is an example of composition for the DiskJockey class. Is that what you mean? I explain a great deal of the jargon in my object oriented design tutorials.
Sorry, but I don't know anything about runescape. I mainly make complicated simulations. That is what I'll base any games I make on. So you can expect to see tutorials on creating everything with OpenGL here instead of what is common on other channels which is to use tools like Unity 3D. I made that decision because there is zero cost involved and the possibilities are endless in regards to the games you can design.
sorry if that questions have already been asked: 1. why don't you use generics instead of casting the components? (e.g. ArrayList songComponents = new ArrayList(); 2. why do you need, for example, getBandName or getReleaseYear, which are only needed in the Leaf-class, in your abstract component class? Can't you just omit them?
wasgibtsnet Only because I tried to keep the code as simple as possible. Generics confuse some people so I decided to avoid them here. Yes they should have been omitted. I largely wrote this out of my head which allows me to better cover the thinking process, but doesn't always lead to perfectly optimized code.
1. You're right, without declaring ArrayList it won't compile anyway. Derek's point below of keeping things simple makes sense. 2. You're right, see discussion above!
To make games you need to learn a good oop language and a low level language. I use java and c. I have a massive java tutorial and tons of videos on object oriented design. If you learn that you'll be able to make anything
Hey Derek first of, thanks for your videos! The only really disturbing sequence is your UML diagram. You declare SongComponent as an interface and implement it as an abstract class, which is "FINE". But in the UML Song doesnt extend or implement SongComponent. Which personally made it hard for me to understand in the first place, but after seeing your implementation not accordingly to your uml pattern and with additional help from wikipedia, I finally got it. keep up the good work, it has been years since this video was made anyway :D
Thank you for the request. I'll add that to the list. I have many more videos on design patterns, oop design and code refactoring coming up. That way you'll be able to write better code. I hope to cover all of the common topics taught to software engineers. Thanks for watching :)
thank you your video. i have a question. my java is not good. In the abstract class songcomponent, why do we need use the abstract class? i see there has not any abstract method.
Thank you :) I must have written that in an odd way? I meant that the song was a leaf. Here is my comment: This acts as an interface for every Song (Leaf) and SongGroup (Composite) we create. Maybe I should describe it differently?
The composite class "SongGroup" inherits the abstract methods getSongName, getBandName, etc. from the "SongComponent" interface which does not make sense for a SongGroup. Is this ok?
I also have this question. Something about making all available methods pooled at the abstract class sounds fishy. Especially since we handle incorrect behavior with exceptions. I think only the shared methods should be in the SongComponent class, but then we have to know beforehand if the object is composite or leaf.
Thank you very much :) I'm happy to help. It's funny how bad those power points are. I have seen the ones they use even in the Ivy league schools. They basically copy and paste information directly from books. I really wonder if the professors understand the information or not?
To get rid of the yellow type warning in Eclipse I had to change 3 things on 2 lines in SongGroup.java ArrayList songComponents = new ArrayList(); and Iterator songIterator = songComponents.iterator();
This is great. Although I don't understand why we need the methods in the parent class, since we just end up making them throw an exception and then overwriting them. Why not only implement it in the subclass? What's the benefit here?
Great tutorial as always. This playlist is a great complement to Head First Design Patterns. My question is how does the iterator go through ALL the songs all at once when you call crazyLarry.getSongList()? Is it some sort of recursive function that prints out all the songs in a songGroup? My confusion is coming up when I look at the diagram at 2:11, you can see there are songGroups within songGroups with songs etc.
GaiiFAFA Thank you :) Everything is a SongComponent including Song and SongGroup. When DiskJockey is created it is passed a SongGroup which includes every Song. When displaySongInfo() is called for the DiskJockey it executes the SongGroups version which iterates through every song. Here is the source code www.newthinktank.com/2012/10/composite-design-pattern-tutorial/
Derek Banas Let me rephrase the question. If you take the songlist as you have it. And now you add a songComponent called Rock with some songs in it to dubstep songComponent. When you call the iterator in songGroup.java, how does the iterator print all the songs in heavyMetalMusic, IndustrialMusic, dubstep and then go Rock in dubstep. Rock is not on the same 'level' as the others. My inexperience is showing as my first encounter with the iterator was in the previous iterator pattern.
What happens if you want to have just 3 individual songs, not being in any group at all. How do you display info for those 3 songs if they are not in any group and you can't iterate? Thanks!
Hi, Thanks for the video :) Your website has code piece for composite pattern, I have one query on that. You wrote below class which extends SongComponent, and SongComponent has methods like getSongName, but you have not implemented it in your SongGroup class. Why? public class SongGroup extends SongComponent {...}
Derek Banas You kind of saved my examen in a few hours, i'm going to bed now lol i had 7 design patterns to study... i think i'll dream in code now lol. Cheers.
So, the UN-overridden methods, will just throw UnsupportedOperationException. For example : Collection method, it has binarysearch(), SortedCollections inherits Collection and overrides binarysearch(), in this case this operation is supported. When, UnSortedCollection inherits Collection, it should first of all, not override the binarysearch() (Since binary search is possible only on sorted collections), therefore throw the UnsupportedOperationException be default. Right??
Hi Derek, Thanks! OOP and Design Patterns are the biggest reason programming is one of my favorite things to do. Just to understand the application of this design pattern a little bit more, do you think Views and ViewGroups in Android make use of the composite design pattern? Thanks!
Tein van der Lugt You're welcome :) Here is a good article on that scn.sap.com/community/developer-center/mobility-platform/blog/2014/01/11/implementation-of-composite-design-pattern-in-android-view-and-widgets
Thanks for the great job. I have a question regarding the "interface" . Why in your code, you comment it as a leaf? should't be the component and song class be the leaf?
hey Derek. first thing - this tutorial is great. awsome!. i have a question about this line of code : while(songIterator.hasNext()) { SongComponent songInfo = (SongComponent) songIterator.next(); songInfo.displaySongInfo(); } i don't understand one thing. you made a songInfo an object of (SongComponent) by casting it. so how exactly songInfo.displaySongInfo() reach the displaySongInfo in the Song class? thanks
+נתן בראל The gist of it is that the compiler does not know that songIterator.next() returns a SongComponent, so you need the cast. That's because the ArrayList wasn't parameterized aka Generics were not used. But the dynamic type of songInfo will still be either Song or SongGroup at runtime, which is when methods are resolved.
I have a question that is not completely related to this video but I couldn't find a video of yours on the matter: Suppose I want to create two methods, one of which requires an argument, and another, which does the same thing, but acts as a default, if there are no arguments supplied, that is the one called, like the Random.nextInt() and Random.nextInt(int MaxValue) methods. How would I do that? I saw you do that with constructors but not methods.
+Mustapha elmadi Thank you :) They are similar. An abstract class can contain concrete functions (non-abtract classes). An interface must contain only abstract classes.
+Swiss Experiments - Castagnola007 I don't know if this is still relavant but interfaces can ONLY have abstract methods that need to be implemented by classes that implement the interface, so no working funcions or attributes. Abstract classes can have attributes, working functions and abstract methods, however you can not use the NEW operater on an abstract class since you need to implement the abstract methods using inheritance.
Hello Derek, I watched your php videos and java. I really want to know how to code RSPS', (Java biased game if you didn't know already). What do you think I should learn; Java or Javascript? I tried reading the code but without any knowledge it's just impossible.
7 років тому+1
Hi Derek Banas! Excellent material, congradulations! I have one question: is it a reason for you use iterator and not a normal loop? Thanks!
I think because when you use array of objects, a loop actually loops multiple times trough the array. Meanwhile the iterator goes trough the array once.
Thats not the reason. The reason is that you don't want the user to know how the songComponent is actually implemented behind-the-scenes, but still want to allow them to iterate through the songs you hold in some way. The client doesn't have an access to the array and shouldn't even know that it exists. So you have to implement an iterator within your class (and here you do the looping. Cause you know what data structure do you use) and provide it to the user. The benefits of it is of course safety and the fact that in the future you can replace your arrayList by any other structure (for example, a hash map) and the user won't even notice that. A year after the question was asked, but maybe it still may help someone.
If,I have to ensure that a particular song can come only under one song group(only one parent), then my add logic would be, public void add(SongComponent comp){ comp.parentNode = this; this.songComponents.add(comp); } My question is, If I have to implement this, then I have to define the "parentNode" variable in abstract class and override it in derived classes. is it the correct way ?? Also, in that I cannot use interface, as I cannot override variables defined in interface.
A good overview of the pattern in the beginning, but I was looking for C++ examples. Maybe change the title to "Composite Design Pattern (Examples in Java)" ?
Can you explain why it's necessary at all to include either the collection-specific or song-specific methods in SongComponent? Surely the only method that should be in that interface is displaySongInfo? Say we add another type of SongComponent which adds more type-specific methods - these really shouldn't be in the SongComponent interface itself when they should only exist in derived classes. If a caller is attempting to call 'add', 'remove' or 'getComponent' on a Song object itself that would surely be indicative of bad design elsewhere...
Hey Derek. Just wanted to say your videos are amazing. Would you consider doing a video tutorial on Node.JS at any point in time? Thanks again for all your videos
Thank you for covering this. I have a question though. Have you made a video on comparing different design patterns? It seems very similar to state design pattern. Let me know.
Hi Derek, First of all a big Thank You for all your tutorial. I follow them regularly. Regarding this, design pattern, I am planning to use this design pattern in my application, where we have Value Objects, which are responsible for the display of the presentation layer. The scenario is that, each Value object can contain other VOs, based on the functionality of the JSP page. For example, Employee JSP contains, Employee Income details, Employer Details and Expense details. All these three objects/VO construct a Employee JSP. At the same time, this whole employee page is a part of another JSP. I am not getting the whole picture of how to construct it. Is it possible for you to explain, how to use this pattern in case of presentation layer using JSP.
Been watching your videos since I was a student, and years later as an engineer they're still relevant and excellent resource material. Thank you for all the work you've done, the community is forever grateful
It is great to know I've been able to help for so long :) I wish you all the best
Thank you :) I'm mainly teaching topics that I guess are so complex that very few people are interested in them. I'm extremely happy with my community. So many people that I meet online are great friends that I wish I could meet in the real world. It is very gratifying for me to be able to help so many nice people!
Watching in 2023. Immortal content on design patterns.
From your tutorials on python and java to developing candy crush as an intern and later when I'll manage good with my tasks I'm going to get a junior software engineer position. I just want to say thank you!
That's Awesome!!! Congratulations on all you accomplished!!!
I really love your tutorials, really helps me out with my Desin Pattern exams.
Sooo much better than all the useless PowerPoints my school is giving me.
Keep up the good work!!
hey messi didn't know you're a programmer too
If you aren't a CS instructor, you should be! I learn more from a few hours watching your videos than my professors can teach in weeks! Your explanations are top notch, keep up the great work.
Thank you very much for the compliment :) I'm not an instructor aside from what I do on UA-cam though.
You can use method overloading with any method. Just make sure that you use arguments with data types in different orders to get it to work
Dude, you're maybe never gonna see this, but thanks a lot for all your design patterns video, I was struggling to understand in college, and my finals are near, this is a big help, much appreciation.
Thank you for taking the time to tell me I helped :) Best of luck on your finals
You guys determine what I do for the most part. I created Asteroids in parts 50 thru 60 in my Java video tutorial. It covers so many things like collision detection, a lot of physics based stuff like lifelike shooting, flying, etc. Have you seen that? When I start making games I'll start with a simple platformer and move up from there. What type of game do you want to see?
Yes Java, C++ and C# are OOP languages. It won't take that long to learn if you do a little every day and make fun projects. I've known people to learn just about everything in 2 months. They still have to use copy and paste code all of the time, but they know how to use Java pretty well
Another great video! Your videos have been a massive help to me over the past few months so I've donated a few quid via your website just to show my appreciation.
Thank you very much :) I'm happy that I was able to help and please don't feel the need to donate.
Mind has been blown with the simpleness you provide. Thank you
Christopher Lariviere Thank you :) I'm happy I could help
Yes I've seen it. I'll eventually cover frameworks and social networks when I get back into PHP. I know I need to improve on that tutorial. Thanks for the request
This version of the pattern is NOT CORRECT. Every method for all classes is crammed into one abstract class where the default implementation is to throw exceptions. This is a very obvious violation of the Interface Segregation Principle, and a good demonstration of an ANTI-PATTERN.
Just define methods where they are supposed to go. SongGroup should have add() and remove(). Song should have getSongName() and getBandName(). An interface or abstract class is needed for the pattern to work, but it should only have the methods that are common to composite objects (SongGroups) and leaf objects (Songs). In this case, that would be one method: displaySongInfo(). Other sources on UA-cam and the internet will demonstrate the pattern correctly.
+Andrew Palmer It is clearly stated in the Design Patterns book that implementing the child management operations in the component instead of in the composite (like Derek did here) is favoring transparency over safety. By default you have the child management operations fail, so that you can catch exceptions when they are called on leaves, (usually indicates a bug anyway), and you override them in the component to actually do what they should do. That way they both composite and leaves appear to have the same interface to the client. If you want to go for safety, sure, do what you said, but don't say that this implementation is actually an ANTI-PATTERN because it's really one of the default ways of implementing this pattern.
For more info, see www.google.be/imgres?imgurl=t0.gstatic.com/images%3Fq%3Dtbn:ANd9GcQYYgj_qlNHWQecLsaHLSQxzb4Cgkh1JjsJAAGlFyh1gtkVnK0J&imgrefurl=books.google.com/books/about/Design_Patterns.html?id%3D6oHuKQe3TjQC%26source%3Dkp_cover&h=800&w=600&tbnid=uGBuMT7EpSNMtM:&tbnh=160&tbnw=119&docid=I8O6u1SkhQlJ1M&itg=1&usg=__XhfV5M9JAwk1Efwq-JH04_BGshY=
+Steve Peros I've heard this argument before, but this so-called "transparency" is not a virtue. "Transparency," in this context, means the same thing as "breaking the type system to no benefit." Clients should not look at two objects the same way if they do not support the same operations. Run-time exception handling is not preferable to compile-time checking, which is why I call this implementation an anti-pattern. If you use the pattern correctly (i.e. the SOLID version of the pattern), your code will just fail to compile if you try to call an invalid method, and run-time exception handling is unnecessary.
This is one of the key advantages of using languages with static type systems. There is no reason not to let the type system do its job. If you feel like Java's type system just gets in your way, you can go look for a video on Python or Ruby (great languages)!
+Andrew Palmer According to you, using this pattern correctly is only implementing the child management operations in the composite then? And you consider type casting to be a better alternative then when a client is working with a component object and wants to access child management methods?
I'm familiar with other languages, but considering this video is a JAVA implementation, I keep my point restricted to JAVA.
+Steve Peros Steve, you have to type-check anyway. Type-checking is preferable to throwing and catching exceptions.
this is a nice thread...I saw this video just now and debated this same topic with my friend...
Dude, you are awesome, I have an exam tomorrow and this thing is just blowing my brain. BTW, Centipede song is awesome.
Thanks!
I was little bit confused when i saw other implementations of this pattern, but with your implementation about the GROUPs and SONGs it was so easy to understand!! Thank you so much :)
I'm happy I could help :)
Thank you very much :) I'm very happy that you enjoy them. Always feel free to ask questions. I'm not that popular so I always answer all my comments
You really deserve a lot more views and subscribers! Most design patterns are very hard for me to understand - not how they are created, but for what kind of problems they can be used - and you are explaining these things really easily using simple examples. Very nice! Please go on with that great work! Suscribed.
Best Java tutorials on the internet . Thank you !!!
Thank you :)
Brilliant humor, I was laughing most of the video because I talk to my code almost identically. Tutorial was great, looking forward to trying it out,
I'm very happy that you enjoyed it :)
U r Awesome dude. It just takes 20 minutes to understand each of ur videos :-).
That really made easy for me :-). Thank u very much.
Thank you :) I'm very happy that you like them.
wow man, thanks! it's really nice to see ppl like you that take the time to do this kind of stuff. (sorry for the bad english)
Thank you very much :) I'm very happy that I was able to help. Good luck on your exam!
Working with design patterns is all about cool tricks :)
PHP isn't an oop language. What I'm covering now is about as complicated as programming gets. C is a low level language that is pretty easy once you understand memory management. C++ is easy after you understand oop. PHP is just a toy in comparison. Start to learn Java and OOD and you'll quickly see the difference
I think modeling files and directories as a composite pattern example of might be more clear than songs and song groups.
Also, should use generics to eliminate casting.
Duude... BREATH! :D
I'd watched a couple of your tutorials the last days. Explanation is good but the emphasizing... It feels like listening to an auctioneer :D
You're very welcome :) I'm glad you enjoyed it
You're very welcome :) I'm glad it helped explain a topic that many find complicated
SongComponent songList is an example of composition for the DiskJockey class. Is that what you mean? I explain a great deal of the jargon in my object oriented design tutorials.
Sorry, but I don't know anything about runescape. I mainly make complicated simulations. That is what I'll base any games I make on. So you can expect to see tutorials on creating everything with OpenGL here instead of what is common on other channels which is to use tools like Unity 3D. I made that decision because there is zero cost involved and the possibilities are endless in regards to the games you can design.
sorry if that questions have already been asked:
1. why don't you use generics instead of casting the components? (e.g. ArrayList songComponents = new ArrayList();
2. why do you need, for example, getBandName or getReleaseYear, which are only needed in the Leaf-class, in your abstract component class? Can't you just omit them?
wasgibtsnet Only because I tried to keep the code as simple as possible. Generics confuse some people so I decided to avoid them here. Yes they should have been omitted. I largely wrote this out of my head which allows me to better cover the thinking process, but doesn't always lead to perfectly optimized code.
Derek Banas okay thanks, I just thought I didn't get some points :)
1. You're right, without declaring ArrayList it won't compile anyway. Derek's point below of keeping things simple makes sense.
2. You're right, see discussion above!
To make games you need to learn a good oop language and a low level language. I use java and c. I have a massive java tutorial and tons of videos on object oriented design. If you learn that you'll be able to make anything
Hey Derek first of, thanks for your videos!
The only really disturbing sequence is your UML diagram. You declare SongComponent as an interface and implement it as an abstract class, which is "FINE". But in the UML Song doesnt extend or implement SongComponent. Which personally made it hard for me to understand in the first place, but after seeing your implementation not accordingly to your uml pattern and with additional help from wikipedia, I finally got it.
keep up the good work, it has been years since this video was made anyway :D
Clearly explained and easy to follow! Thanks a lot, ur design pattern tutorials turly helped me a lot.
Thank you :) Yes I'm learning the importance of keeping the code as brief as possible
I like the composite pattern. Useful for menus with menu items as well as sub-menus!! Good video!!
Thank you :) When you find the perfect place to use them the patterns are amazing.
I always eventually fulfill all requests. It helps me a lot to know what you guys want me to cover. Thanks
Thank you for the request. I'll add that to the list. I have many more videos on design patterns, oop design and code refactoring coming up. That way you'll be able to write better code. I hope to cover all of the common topics taught to software engineers. Thanks for watching :)
really thank you, i was very helpful
I have too many problems with English language but i understood you well :) Thanks
Your english is great. You are very welcome :) Thank you for taking a look at my videos
You're very welcome :) Good luck on the test.
In hte overview in the beginning SongComponent is defined as an interface, but when you write the code you write it as an abstract class. Why?
I used abstract class because I decided I wanted to throw UnsupportedOperationException()
Thank you very much :) I enjoyed making this series
Hey, this playlist is great!
I have a question.
What do you mean with "Leaf" (At 1:30), I didn't get it.
Diego Aguiar Thank you :) Each Song object is a leaf, or node that makes up the SongGroup. It is a metaphor for a leaf on a tree
Ohh, I get it now. Thank you bro
Thank God you have a good accent. Thanks mate. 🥴♥️
thank you your video. i have a question. my java is not good. In the abstract class songcomponent, why do we need use the abstract class? i see there has not any abstract method.
That was just a slip up on my part. I was writing out of my head. i should have made it an interface.
Thank you :) I must have written that in an odd way? I meant that the song was a leaf. Here is my comment: This acts as an interface for every Song (Leaf) and SongGroup (Composite) we create. Maybe I should describe it differently?
The composite class "SongGroup" inherits the abstract methods getSongName, getBandName, etc. from the "SongComponent" interface which does not make sense for a SongGroup.
Is this ok?
I also have this question. Something about making all available methods pooled at the abstract class sounds fishy. Especially since we handle incorrect behavior with exceptions. I think only the shared methods should be in the SongComponent class, but then we have to know beforehand if the object is composite or leaf.
Thanks a lot. This term i have learned prolog php and software design courses, your video is extremely helpful.
+Tony Sun You're very welcome :)
Thank you very much :) I'm happy to help. It's funny how bad those power points are. I have seen the ones they use even in the Ivy league schools. They basically copy and paste information directly from books. I really wonder if the professors understand the information or not?
I'll definitely make a 3D game. I want to keep it small though so it will work on tablets as well. We'll see what happens
I love the way you pronounce “data”
That’s funny
Hi Derek, why is SongComponent not an interface?
To get rid of the yellow type warning in Eclipse I had to change 3 things on 2 lines in SongGroup.java
ArrayList songComponents = new ArrayList();
and
Iterator songIterator = songComponents.iterator();
+wjrasmussen666 You implemented generics. This will make the cast of songIterator.next() obsolete.
How can you remove Headhunter song from IndustrialMusic list of songs?
This is great. Although I don't understand why we need the methods in the parent class, since we just end up making them throw an exception and then overwriting them. Why not only implement it in the subclass? What's the benefit here?
Great tutorial as always. This playlist is a great complement to Head First Design Patterns.
My question is how does the iterator go through ALL the songs all at once when you call crazyLarry.getSongList()? Is it some sort of recursive function that prints out all the songs in a songGroup?
My confusion is coming up when I look at the diagram at 2:11, you can see there are songGroups within songGroups with songs etc.
GaiiFAFA Thank you :) Everything is a SongComponent including Song and SongGroup. When DiskJockey is created it is passed a SongGroup which includes every Song. When displaySongInfo() is called for the DiskJockey it executes the SongGroups version which iterates through every song. Here is the source code www.newthinktank.com/2012/10/composite-design-pattern-tutorial/
Derek Banas
Let me rephrase the question. If you take the songlist as you have it. And now you add a songComponent called Rock with some songs in it to dubstep songComponent. When you call the iterator in songGroup.java, how does the iterator print all the songs in heavyMetalMusic, IndustrialMusic, dubstep and then go Rock in dubstep. Rock is not on the same 'level' as the others. My inexperience is showing as my first encounter with the iterator was in the previous iterator pattern.
GaiiFAFA To have another addition auto iterate you'll need to have added it to the SongGroup
Very Good video .Thanks for Explanation. Hi Derek, can you please correct me whether Directory of files/folders follow the Composite pattern.
+naveen kumar Thank you :) Yes you could definitely structure data in that way using this pattern.
nice vidoe, but i still have a question
on what kind scenario should i use this pattern?
What happens if you want to have just 3 individual songs, not being in any group at all. How do you display info for those 3 songs if they are not in any group and you can't iterate? Thanks!
You have saved my life good sir
I'm very happy you enjoyed the video
@@derekbanas One question, aren't you supposed to declare the methods in the superclass as abstract? At least some of them anyway?
@@prassanak3601 Not a necessity, but you could if you wanted to.
Usted es muy agradable
Hi,
Thanks for the video :)
Your website has code piece for composite pattern, I have one query on that.
You wrote below class which extends SongComponent, and SongComponent has methods like getSongName, but you have not implemented it in your SongGroup class. Why?
public class SongGroup extends SongComponent {...}
You need more views... very nice thanks!
Thank you :) You're very welcome.
Derek Banas You kind of saved my examen in a few hours, i'm going to bed now lol i had 7 design patterns to study... i think i'll dream in code now lol. Cheers.
Jepix X Best of luck on your exam :)
Thanks alot for these videos. Helps me alot preparing for my test!!
So, the UN-overridden methods, will just throw UnsupportedOperationException.
For example : Collection method, it has binarysearch(), SortedCollections inherits Collection and overrides binarysearch(), in this case this operation is supported.
When, UnSortedCollection inherits Collection, it should first of all, not override the binarysearch() (Since binary search is possible only on sorted collections), therefore throw the UnsupportedOperationException be default. Right??
very useful pattern, and nicely explained. Thanks!
Thank you :)
can i have different types of leaf class besides song ?
or it just either group or song
very informative. Thanks for the videos Derek!
You're very welcome :)
+Derek Banas I do not understand. How can a SongGroup contain a SongGroup or SongGroup and leafs?
is composite pattern like multiton pattern or not ?
if not what the difference thnx
When will the exception be thrown? Who defines the condition? I did not get that. Will you please explain?
I did my best to make it easy to translate into most any OOP language. Sorry it didn't help
Hi Derek,
Thanks! OOP and Design Patterns are the biggest reason programming is one of my favorite things to do.
Just to understand the application of this design pattern a little bit more, do you think Views and ViewGroups in Android make use of the composite design pattern?
Thanks!
Tein van der Lugt You're welcome :) Here is a good article on that scn.sap.com/community/developer-center/mobility-platform/blog/2014/01/11/implementation-of-composite-design-pattern-in-android-view-and-widgets
Ah, I see why you used displaySongInfo() for SongGroup and Song, pretty cool trick.
Thanks for the great job. I have a question regarding the "interface" . Why in your code, you comment it as a leaf? should't be the component and song class be the leaf?
what is difference between composite and decorator pattern?
hey Derek. first thing - this tutorial is great. awsome!.
i have a question about this line of code :
while(songIterator.hasNext()) {
SongComponent songInfo = (SongComponent) songIterator.next();
songInfo.displaySongInfo();
}
i don't understand one thing.
you made a songInfo an object of (SongComponent) by casting it.
so how exactly songInfo.displaySongInfo() reach the displaySongInfo in the Song class?
thanks
+נתן בראל The gist of it is that the compiler does not know that songIterator.next() returns a SongComponent, so you need the cast. That's because the ArrayList wasn't parameterized aka Generics were not used. But the dynamic type of songInfo will still be either Song or SongGroup at runtime, which is when methods are resolved.
I have a question that is not completely related to this video but I couldn't find a video of yours on the matter:
Suppose I want to create two methods, one of which requires an argument, and another, which does the same thing, but acts as a default, if there are no arguments supplied, that is the one called, like the Random.nextInt() and Random.nextInt(int MaxValue) methods. How would I do that? I saw you do that with constructors but not methods.
Excellent video just one note you said an interface and you used an abstract class is it the same ?
+Mustapha elmadi Thank you :) They are similar. An abstract class can contain concrete functions (non-abtract classes). An interface must contain only abstract classes.
+Derek Banas isn't this false in java 1.8? as far as i know you can write concrete functions in interfaces as well now.
+Swiss Experiments - Castagnola007 I don't know if this is still relavant but interfaces can ONLY have abstract methods that need to be implemented by classes that implement the interface, so no working funcions or attributes. Abstract classes can have attributes, working functions and abstract methods, however you can not use the NEW operater on an abstract class since you need to implement the abstract methods using inheritance.
Actually in Java 8 you can have interfaces with default implementations. you can look it up if you're interested.
Clearly explained
we played a drinking game where we play this video and take a shot every time you say song
we are in the hospital
That’s funny :D
Hello Derek,
I watched your php videos and java.
I really want to know how to code RSPS', (Java biased game if you didn't know already). What do you think I should learn; Java or Javascript?
I tried reading the code but without any knowledge it's just impossible.
Hi Derek Banas! Excellent material, congradulations!
I have one question: is it a reason for you use iterator and not a normal loop?
Thanks!
I think because when you use array of objects, a loop actually loops multiple times trough the array. Meanwhile the iterator goes trough the array once.
Thats not the reason. The reason is that you don't want the user to know how the songComponent is actually implemented behind-the-scenes, but still want to allow them to iterate through the songs you hold in some way.
The client doesn't have an access to the array and shouldn't even know that it exists. So you have to implement an iterator within your class (and here you do the looping. Cause you know what data structure do you use) and provide it to the user.
The benefits of it is of course safety and the fact that in the future you can replace your arrayList by any other structure (for example, a hash map) and the user won't even notice that.
A year after the question was asked, but maybe it still may help someone.
You are amazing, Derek!
Thank you for the nice compliment :)
Very good video. Thank you for this.
Thank you :)
Loved crazyLarry better than madMike. Great tutorial, thanks!!
That's funny :) Thank you
If,I have to ensure that a particular song can come only under one song group(only one parent), then my add logic would be,
public void add(SongComponent comp){
comp.parentNode = this;
this.songComponents.add(comp);
}
My question is, If I have to implement this, then I have to define the "parentNode" variable in abstract class and override it in derived classes. is it the correct way ??
Also, in that I cannot use interface, as I cannot override variables defined in interface.
Thanks for coming by SawMan :)
NIN and Front 242!!! they are really cool bands and I´m learning design Patterns too!! : D.
That's cool. I'm glad you are enjoying the videos :)
I'm happy I could help :)
how come songComponents are ArrayLIst instead of HashSet? shouldnt there be unique song?
could we use interface instead of abstract class?
A good overview of the pattern in the beginning, but I was looking for C++ examples. Maybe change the title to "Composite Design Pattern (Examples in Java)" ?
Can you explain why it's necessary at all to include either the collection-specific or song-specific methods in SongComponent? Surely the only method that should be in that interface is displaySongInfo? Say we add another type of SongComponent which adds more type-specific methods - these really shouldn't be in the SongComponent interface itself when they should only exist in derived classes. If a caller is attempting to call 'add', 'remove' or 'getComponent' on a Song object itself that would surely be indicative of bad design elsewhere...
Hey Derek. Just wanted to say your videos are amazing. Would you consider doing a video tutorial on Node.JS at any point in time? Thanks again for all your videos
Armaan Dhanji Thank you :) Yes NodeJS tutorials will start as soon as I finish up Rails.
This video is great. Helped me a lot!!! Thanks!!
Are you a crazy successful software designer now?
Thank you for covering this. I have a question though. Have you made a video on comparing different design patterns? It seems very similar to state design pattern. Let me know.
Great video Derek thank you!
Thank you :) I'm glad it helped
Hi Derek,
First of all a big Thank You for all your tutorial. I follow them regularly. Regarding this, design pattern, I am planning to use this design pattern in my application, where we have Value Objects, which are responsible for the display of the presentation layer. The scenario is that, each Value object can contain other VOs, based on the functionality of the JSP page. For example, Employee JSP contains, Employee Income details, Employer Details and Expense details. All these three objects/VO construct a Employee JSP. At the same time, this whole employee page is a part of another JSP. I am not getting the whole picture of how to construct it. Is it possible for you to explain, how to use this pattern in case of presentation layer using JSP.