Strategy Design Pattern
In this article we are implementing Strategy design patter in our project
Design Pattern Overview
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.
Types of Design Patterns
Strategy Pattern
In Strategy pattern, a class behavior or its algorithm can be changed at run time.
Advantages
More clean code because you separate the concerns into classes (a class to each strategy).
It’s easy to switch between different algorithms (strategies) in runtime because you’re using polymorphism in the interfaces.
Implementation
We used spring boot project for implementing strategy design pattern. You can use simple java project for implementing strategy design pattern.
Let’s create a new simple project at https://start.spring.io/
We need common dependencies as
First of all we are gonna create an interface with name IFileParserStrategy.
In this article we examine file opertaions like parsing html,parsing json etc. We think that this way it will be more understandable article. After created interface now we need an enum class for file types.
Now let’s create parser classes and implement IFileParserStrategy.
Html parser class;
Json parser class;
Xml parser class;
After created strategy classes now ve need a context class which determining correct type of strategy. We are gonna create FileParserStrategyService class.
As you can see we put strategy types in a map and returning correct strategy type by file type as IFileParserStrategy.
Finally we are created strategy pattern. Now let’s test this pattern with Junit.
You can find all project details at github.
Contibutors
Bahattin Yılmaz => Github, Linkedin, Medium
Faruk Karadeniz => Github , Linkedin