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

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