Strategy Design Pattern implementation in Spring boot Java | Interview Questions | Code Decode
Вставка
- Опубліковано 16 жов 2024
- Live demo of how to Implement Strategy Design Pattern in Java in your project with real world example
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
openinapp.co/u...
Course Description Video :
yt.openinapp.c...
What is Strategy Design Pattern in java?
Strategy design pattern is a behavioral design pattern that enables selecting an algorithm at run-time. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.
Why is Strategy Design Pattern Needed at all in java spring Boot
Typically, we tend to bundle all the algorithm logic in the host class, resulting in a monolithic class with multiple switch case or conditional statements. The following example shows the structure of one such class that supports multiple algorithms to encrypt data. family of algorithms to use.
At runtime, the code loops through the statements to perform encryption based on the client specified algorithm. The result is a tightly coupled and rigid software that is difficult-to-change.
You can imagine the consequences if you try to implement a new encryption algorithm, say TripleDES or a custom encryption algorithm.
You’d have to open and modify the Encryptor class. Also, if an existing algorithm needs to be changed, the Encryptor class will again require modification.
As you can see, our Encryptor class is a clear violation of the Open Closed principle - one of the SOLID design principles. As per the principle, new functionality should be added by writing new code, rather than modifying existing code.
You can avoid them by using the Strategy pattern
How does Strategy Design Pattern Work?
We define one abstracting Interface and multiple implementing classes
Clients can choose the algorithm to use at run time. Like adding it in properties file and reading at runtime, thereby deciding which also to pick at run time only
Each of the algorithm classes adheres to the Single Responsibility principle, another SOLID principle as they will only be concerned with encrypting data with a specific algorithm, which is currently lacking.
Participants of Strategy Design Pattern?
Common interface.
Multiple implementing standalone classes.
Create Enum with Multiple encryption types.
Strategy Factory
Real time use of Strategy Design Pattern?
Sorting class that supports multiple sorting algorithms, such as bubble sort, merge sort, and quick sort.
a file compression class can support different compression algorithms, such as ZIP, GZIP, and LZ4, or even a custom compression algorithm
data encryption class that encrypts data using different encryption algorithms, such as SHA1 SHA2 MD5, etc.
Most Asked Core Java Interview Questions and Answers: • Core Java frequently a...
Advance Java Interview Questions and Answers: • Advance Java Interview...
Java 8 Interview Questions and Answers: • Java 8 Interview Quest...
Hibernate Interview Questions and Answers:
• Hibernate Interview Qu...
Spring Boot Interview Questions and Answers:
• Advance Java Interview...
Angular Playlist: • Angular Course Introdu...
SQL Playlist: • SQL Interview Question...
GIT: • GIT
Subscriber and Follow Code Decode
Subscriber Code Decode: www.youtube.co...
LinkedIn : / codedecodeyoutube
Instagram: / codedecode25
#designpatterns #codedecode #strategydesignpattern
Wow, I felt very blessed that I found your channel. The way you teach this by following the industry standard code using spring boot is very very appreciable. Thank you for your channel, very very less instructor gave such thorough knowledge. This is the real teaching, which works for everyone, even after getting a good job. Thanks alot Ma'am for bringing such good content.
Thanks a lot Abhishek 🙂🙂
Thank you mam.. 🙏🙏
Because of you, I was able to switch my job and cleared all of my interviews.
I literally watched a lot of your videos multiple times which really helped me a lot...
Many congratulations on cracking interviews Man. 🎉🎉🎉🎉 Thanks A Lot Ankur 🙂🙂
Mindblowing! Great effort! Crystal Clear explanation, As mentioned by others in comments I have gone through many other channel videos, Never seen this kind of masterpiece! I am watching your videos regularly, Keep doing many more videos, I have suggested your channel to my friends and even my juniors to learn.
Thanks a lot 🙂🙂. Glad to have u around Man 👍👍
Maybe I am wrong but you still have to modify existing enum thus you have to recompile.
If this was implemented with if-then or switch you would have to add a single else if statement in a single file and recompile and both of these two changes (enum or if-else) are quite trivial.
Open-closed principle is not satisfied in neither case.
It's just that the problem is moved from one place to another, but centralized.
Proper implementation (one not breaking open-closed) would require no changes to existing code - just adding the new class implementing that interface should be enough.
How should we do this? I was thinking during runtime, get the bean of the chosen encryption using qualifier.
Very effective & to the point, thanks for all the videos. Please post your git hub repository link.
Thanks for the great content! 😊
Also, Please create complete video on spring boot project creation from start to end with all the required validation, authentication following design patterns.
It would be really helpful for quick revision as well as to understand things deeply.
Sure Karishma👍
after watching/reading about the pattern from other sources on internet i thought i got it.
but after watching your video only i understood what problem this pattern solves, how it can be applied in an enterprise grade project.
Excellent work. I have never seen this type of explanation. 🔥🔥🔥
i like the way the below things are covered in your video.
what is it
why it is used instead of if else based approach.
how it is used
debugging the usage.
Hi, thanks a lot for making these videos. Please make video on all design patters of java, with example as you have explained in this video
Awesome.. Interviewer asked this pattern.. Thank you.!!
Thanks 🙂🙂
you are awesome.. understood this . I am using this in my project 👍👍. thank you
Yeah most good projects use this design pattern. It's a must for best practice implementation in java projects 🙂🙂
Thanks for the Explanation, one Q - at 16:03 when component is loaded and the constructor is called how is the treeset value being passed ?
It's constructor injection. If u see it's @autowired 👍👍
The identity provider or Authentication Server accepts the JWT token and decodes it to find the algorithm used in the header.
IDP uses the same algorithm for token signature validation, it's a use case for Strategy Design Patterns as the algorithm is not pre-decided.
Wow ma'am you are amazing
Thanks Devendra 😊😊
Awesome Explanation
Thanks
Awesome explanation. Requesting you to do other patterns i had faced Adaptor, Command ,Template design pattern these are asked in jpmorgan and goldman sach. All finance companies concentration on these i hope you will respond to this chat. Thanks for your effort and time.
Sure Mahi we will try to upload them . Was it asked bcz u used them in any project or out of blue moon they asked random patterns ?
Can you please make a video on Junits and Mockito? If possible, provide real-life examples of testing REST web services.
Mam pls complete last JUnit series... JUnit for controller n all... Those are pending...thank u.. gr8 work..
Keep up.
Sure 👍👍 we will do that👍
Well explained. Thanks for sharing.
You're welcome amit
nice topic😊😊 become a fan of your🥰
Thanks 🙂🙂
Keep up the good work
Thanks Sayan 🙂🙂
Welcome back...
Thanks Suresh 🙂. We never went anywhere. 🙂🙂
I do have a problem, there is NullPointerException in my application. My encryptionTypesSet argument in Autowired method EncryptionFactory is null and therefore I cannot create any strategy. Does anyone have an idea what the cause could be?
You forgot @autowire on constructor of factory class?
where will get code for above Example
Hi Mam, Can you please Cover Ambassador Design Pattern of Microservices?
Sure 👍👍
Thanks a million
🙂🙂 no problem Rishi
Mam microsevices m scenario based questions p ek interview video upload kijiye
Sure 👍👍
Pls have more videos on all design patterns
Sure 👍👍
hello madam kindly make a video on junit and mockito
Please make a video on visitor design pattern
Sure we will create it soon
please create video for @transaction in spring boot
Sure we will do that👍
Hi can you please take one vedio webclient using webflux plz
Sure 👍👍
should this playlist contain more videos? of other common patterns?
Sure Ashutosh. Can you please let us know which all patterns you need. We will put videos on them too
@@CodeDecode Behavioural patterns like Observer pattern, creational pattern like builder, structural pattern like adapter, bridge, decorator, facade, some J2ee patterns . This is just so that the playlist is the one place to go for common design patterns. Appreciate your work. Amazing channel
Sure Ashutosh we will create more dp then. Builder is there in this playlist though. Check 32.50 min of the first part. Lemme know if you still can't find it 👍👍🙂
please provide git link sister