Strategy Design Pattern implementation in Spring boot Java | Interview Questions | Code Decode

preview_player
Показать описание
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:

Course Description Video :

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.

Hibernate Interview Questions and Answers:

Spring Boot Interview Questions and Answers:

Subscriber and Follow Code Decode

#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.

abhishekverma
Автор

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.

senthilrajatcs
Автор

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.

tejeswarpala
Автор

Very effective & to the point, thanks for all the videos. Please post your git hub repository link.

vishalsepaia
Автор

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

artikumari-esep
Автор

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.

bistabil
Автор

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?

hassanwhiteside
Автор

please create video for @transaction in spring boot

hamednikbakht
Автор

hello madam kindly make a video on junit and mockito

revanthchinnu
Автор

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.

mahi
Автор

Mam pls complete last JUnit series... JUnit for controller n all... Those are pending...thank u.. gr8 work..
Keep up.

raakesh
Автор

Mam microsevices m scenario based questions p ek interview video upload kijiye

LifeTracksVlogs
Автор

you are awesome.. understood this . I am using this in my project 👍👍. thank you

travelwithgattu
Автор

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 ?

sandeepchebrolu
Автор

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.

karishmarawat
Автор

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...

ankursingh
Автор

Hi Mam, Can you please Cover Ambassador Design Pattern of Microservices?

amitrangwani
Автор

Hi can you please take one vedio webclient using webflux plz

koraboinaraju
Автор

Awesome.. Interviewer asked this pattern.. Thank you.!!

kdautomotive
Автор

Please make a video on visitor design pattern

akashpawar