Рет қаралды 24,270
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/udemycourse
Course Description Video :
yt.openinapp.co/dmjvd
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: kzfaq.info?...
LinkedIn : / codedecodeyoutube
Instagram: / codedecode25
#designpatterns #codedecode #strategydesignpattern