Strategy and Factory patterns are the most useful design patterns in any application. If you have a branching code with lots of ‘if and else’, then Strategy pattern is the best choice. With Spring Boot’s powerful dependency injection, Factory pattern comes to the rescue when different sub types of objects are needed. This is a continuation of Part I of the blog series, which detailed the proper use of the Factory Pattern.
The starting point for Strategy Pattern is an interface that has two methods. One method is to get the Report Id Enum and another method is to get the actual report data based on the user id. I created a default method and added code to get some mock data for each report id before the actual implementation is done. When the actual implementation is done, this default method will no longer be used. Also, having a default method ensures backward compatibility without breaking the code.
Service classes: one each for one Report Id in the Enum class
The screenshot below shows two service classes: one each for one Report Id Enum class that implements Report Interface for Engine.
Initially, I have not added the actual implementation in the Service class. This would work fine as we have a default method in the interface that returns the mock data from a JSON file.
Service classes with actual implementation
The screenshot below shows the Service call with an actual implementation, which will circumvent the default method in the interface.
UML Diagrams of Service classes
A combination of Factory and Strategy pattern is a great gift when designing solutions in a clean way without having to have the clumsy ‘if and else’ in the code. Part I of this series explained Factory pattern usage in a Spring Boot application. This post explored how Strategy pattern is applied alongside Factory Pattern. Thanks for reading! If you continue to have any questions around Factory and Strategy patterns, please don’t hesitate to reach out to us for answers.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
|cookielawinfo-checbox-analytics||11 months||This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".|
|cookielawinfo-checbox-functional||11 months||The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".|
|cookielawinfo-checbox-others||11 months||This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.|
|cookielawinfo-checkbox-necessary||11 months||This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".|
|cookielawinfo-checkbox-performance||11 months||This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".|
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.