Тёмный

The Command Pattern Explained and Implemented in Java | Behavioral Design Patterns | Geekific 

Подписаться
Просмотров 54 тыс.
% 959

Support us on Patreon: www.patreon.com/Geekific
GitHub Repository: github.com/geekific-official/geekific-youtube/
Stay updated on our videos by subscribing to the channel and hitting the like button!
In this video, we break down, explain and implement in Java the Command Behavioral Design Pattern.
Timestamps:
00:00 Introduction
00:08 Practical Use-Case
01:11 Command Pattern Implementation
04:04 What is the Command Pattern?
04:28 Command Pattern Class Diagram
06:15 Recap
06:55 Thanks for Watching!
If you found this video helpful, check other Geekific uploads:
- Introduction to Design Patterns: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-mE3qTp1TEbg.html
- The Factory Method Pattern Explained and Implemented in Java: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-EdFq_JIThqM.html
- The Abstract Factory Pattern Explained and Implemented in Java: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-QNpwWkdFvgQ.html
- The Singleton Pattern Explained and Implemented in Java: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-tSZn4wkBIu8.html
- Object-Oriented Programming Fundamentals: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Vfk6sExu8-4.html
- SOLID Principles and Best Practices: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-HoA6aZPR5K0.html
#Geekific #DesignPatterns #CommandPattern #BehavioralPatterns

Опубликовано:

 

3 июл 2021

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 42   
@lucasrubio7664
@lucasrubio7664 Год назад
I've been watching some of your Design Patterns videos for a uni exam. Great explanations with really good visual examples. They are very helpful thank you!!
@carlosalejandromeliscurbel2679
Great job. I can say I'm learning more with your videos than going to class. In 7 minutes you accomplished what my teacher tried in 45minutes😂
@seranvishwa8714
@seranvishwa8714 5 месяцев назад
Same in my case😂
@AnkitPandey-ft8jb
@AnkitPandey-ft8jb 10 месяцев назад
Your explanations are most relevant and spot on! Gives the gist of every pattern in very little time. Thanks for doing this.
@charlesopuoro5295
@charlesopuoro5295 7 месяцев назад
Thanks always for these insightful, scenario-based videos that help us grasp these Software Design Patterns.
@elvenpath7
@elvenpath7 Год назад
Geekific, thanks for what you are doing ! You videos are so cool.
@babelwebdeveloper2240
@babelwebdeveloper2240 2 года назад
Awesome work! Such content is very necessary, don't stop doing what you do!
@geekific
@geekific 2 года назад
Thanks a lot, glad you like it, will do!
@Hermioneswand1
@Hermioneswand1 2 года назад
Wonderfully explained, thanks! Going to explore all your other videos!! Keep going. Thank you
@geekific
@geekific 2 года назад
Awesome, thanks for the support!
@parinjasoliya7836
@parinjasoliya7836 9 месяцев назад
Very Good explanation!
@yairlevi8469
@yairlevi8469 Год назад
Amazing content
@GreyDeathVaccine
@GreyDeathVaccine Год назад
You have earned a sub with down to Earth example 🙂
@geekific
@geekific Год назад
Thank you for the support :)
@lelo8244
@lelo8244 2 года назад
Literally the best! Tysm
@geekific
@geekific 2 года назад
Glad it helped :)
@TheClockmister
@TheClockmister Месяц назад
everyone gangsta until sander death stares you
@mrjessan2343
@mrjessan2343 5 месяцев назад
Thank you for the video but in 4:35 uml diagram you draw the arrow to invoker from client. But in the other resources there is no connections between them. Is it mistake or what?
@harshmalra3085
@harshmalra3085 2 года назад
best content.
@geekific
@geekific 2 года назад
So glad you are enjoying our videos :)
@Maik55732
@Maik55732 Год назад
First of all, thank you for the explanation, it was all well presented. However, I still have few questions: 1. I don't see how the code prior the pattern is that much coupled. Could please point out some dependencies or changes that could be made on the Light class that would lead to crash in the subclasses? 2. It's not clear for me where I can use this pattern. Any system where I have a command? Perhaps it was too much to ask, but if anyone can help me out with this, I'd be much appreciated.
@geekific
@geekific Год назад
Hello! Glad you liked it! 1- In the first implementation, everytime you add stuff to your Room (like Lights) all subclasses are affected, and as the list of attributes added grow, these Rooms will be handling many responsibilities. In addition to this they are aware of these functionalities. When we used the command, this became generic, the Rooms are not aware anymore of what is being executed, just that it can execute something, and all the code related to Lights for example is now encapsulated in its own class (this is explained from 2:50 to 4:00 if you want to check it out again) 2- The command pattern decouples the classes that invoke the operation from the object that knows how to execute the operation. One use-case for using this pattern is that the executor of the command doesn't need to know anything about what the command is executing, what attributes it needs or what it actually does, as that is encapsulated in the command. This allows you to have a list of commands that are executed in order for example, that are dependent on other items without being dependent on the executor, that are triggered based on a certain event... Hope this helps! Cheers!
@genatokarev810
@genatokarev810 Год назад
Thank you. Love your videos so much. Have a question. How can we know that the LivingRoom's lights are on? I suppose we should have created something like livingRoomLights variable instead of just passing `new Light()` so we could have known that from the client code. Or wrap all of that with another class around the livingRoom and the livingRoomLights objets.
@geekific
@geekific Год назад
Glad you do! Thank you :) This code is strictly to demonstrate the pattern you can expand on it as you wish.
@LordErnie
@LordErnie 7 месяцев назад
I'm sorry to say that the command pattern, like many other OO patterns, is just one prime example of how OO fcks itself by not treating functions as assignable values. What is a command? It's one hell of a weird way to define a function abstraction. Here is this thing that will call a function if you poke it. Oh yea the function can change, and the thing you are poaking abstracts away what the concrete function is. "But commands are objects that can have state and yayayayayyayadaydadada". Yes, but when you just look at the invoke function in is a function that does something before, maybe during, and after a dynamic function call. You might as well just have a wrapper function composed with a predefined state context. Is this hard? No. Is it just a fancy way of making a function return a function, and calling that function? Yes. Bohooo a returns b composed with parameter c from a. Call b, and it does the spookies. I do see how the command pattern has its usages, but it's an ugly way of saying that you can't pass functions as values, and that you can't produce them either. It's a fckd wrapper. But nice vid tho very informative, keep it up ♥
@SumTingWong886
@SumTingWong886 7 месяцев назад
Yeah I’m not reading all that, but I’m happy for you or sorry that happened.
@shankar7435
@shankar7435 5 месяцев назад
Thanks for such insights to know what I am getting into.
@davidblbulyan3077
@davidblbulyan3077 9 месяцев назад
3:56 it is strange, because we pass some light in SwitchLightsCommand, but this light, created in the previous line, is not a light of the room
@dee4622
@dee4622 7 месяцев назад
"private Light light" in SwitchLightsCommand is a pointer to Light object not an actual Light object. It doesn't point to any Light object until "new SwitchLightsCommand(new Light())" in the main function. "new" indicates that your dynamically creating the actual object and then that is being passed into the SwitchLightsCommand constructor.
@sdsd-ec8rw
@sdsd-ec8rw Год назад
you use classes on slides but in the repository there are stupid records which does not compile because of java version. was that so necessary switching from classes to records and upgrading version of jvm?
@geekific
@geekific Год назад
Hello! If you are using IntelliJ, you can easily convert from records to classes using the drop down options of the Alt+Enter menu :)
@sdsd-ec8rw
@sdsd-ec8rw Год назад
@@geekific would have been much better if your slides and your repo were 100% synchronized. Many classes you show on the slides are not even present in the repo. And again, using latest features of the language for rather simple tiny project is not the best choice.
@avegeta1
@avegeta1 4 месяца назад
I am just wondering, with the command pattern setup, Now Livingroom and Its Light object are not related anymore. How do we tend to preserve the relationship here. To my doubt, I believe we don't have relationships here is because its a kind of a Behavioral pattern and not Creational pattern. Please correct my understanding, I am here to learn.
@geekific
@geekific 4 месяца назад
Your understanding is on the right track. In the context of the Command Pattern, relationships between objects are not necessarily established through direct associations in the traditional sense. Instead, it promotes a more flexible and loosely coupled structure, where the sender does not need to know the specific class of the receiver. (Maybe the nature of the example here confused you a little). The Command Pattern doesn't explicitly deal with establishing relationships between objects in the way Creational Patterns might, it indirectly influences the interactions between objects by decoupling the sender and receiver.
@cparks1000000
@cparks1000000 Год назад
I think the example is slightly unrealistic as one would unlikely make a class for each room in a house, since the developer has no idea what kind of rooms will be in the user's house. (What if the user has a "sun room" for example or if the "room" is actually an outside space such as a pool area.) I think this is unrealism is a distraction from the core message. It would make more sense for a room to have a name attribute and a list of "things". Alternatively, you could just use a different example. It's also weird that the room has no reference to the objects themselves. For example, there's no way to ask a room if a light is on or off. This information would be essential to making a user-interface phone app. For the curtain example, you might want extra functionality. Perhaps you want the curtains to be 50% open or 33% open. However, this functionality isn't compatible with a (non-dimming) light as on/off is boolean. You can further imagine a light that can change colors. We would now need to pass 2 numbers (hue and saturation) to specify a given color (assuming you want brightness to be independent of color) In general, it seems better to send a command somehow, but I can't see exactly how that would work.
@geekific
@geekific Год назад
I remember that coming up with an easy example that allows us to focus on the pattern more than the actual use-case was hard here. I hope we were able to relay this information regardless of the messed up use-case xD
@tommyholmberg
@tommyholmberg Год назад
I like your presentation but the pattern still makes little sense to me. Looking at the code on time 4:00, you still have the exact same thing as on time 0:55.
@geekific
@geekific Год назад
Thanks. The difference is that at 0:55 the rooms are aware of lights, however, at 4:00 they are decoupled, you can pass in any type of command you want. And of course they might seem similar, because the idea is not to change the functionality, this has to remain the same, all we want is aim for a cleaner code.
@mehdi-vl5nn
@mehdi-vl5nn 9 месяцев назад
Srp be like😮😮😮
@paulchaperon2207
@paulchaperon2207 3 месяца назад
OOP is a mental asylum
@kennethcarvalho3684
@kennethcarvalho3684 10 месяцев назад
Little bit complicated
@geekific
@geekific 10 месяцев назад
Please let us know which parts were the most confusing so we may try to help in the comments :) Cheers!