Marcus Lee Eugene
2 min readOct 20, 2021

--

Well done Junhua, this article was very insightful. I have done something similar like this in one of my projects and I did not know that this implementation was called strategy pattern. I felt like this article explained the concepts very well, lots of analogy and examples with sample codes to help me understand the concept better. The advantages and disadvantages section was helpful as it gave me a sense of the trade offs which will enable me to decide when I should use the pattern in my next project. It was a very smooth transition to show the “Problem” then “Solution” and allowed me to see the pattern’s effectiveness.

The idea I got from this article about Strategy pattern is that it allows for the program runtime to decide from a category of objects, what specific type of object it belongs to the category to output. This reduces the need to create multiple duplicate function codes which perform the same actions but working with different objects. It reduces the number of lines of codes, makes the codebase more clean and readable. I really liked the hamburger analogy in the article and would like to make an example out of it. If no strategy pattern was used, there would be many “Burger” functions that would have to be called in order to make the burger (CheeseBurger, Chicken Burger etc.). However, with a single “Burger” function, the user can feed the desired “meat” piece into the “Burger”, without having to duplicate multiple “Burger” functions that has very little difference in their codes.

I believe that the Strategy pattern is similar to the State Pattern in the sense that it changes the object’s method dynamically and makes an object change its behaviour when the internal state changes. State pattern is used a lot in Game Development where the state of the game can be changed depending on how the gamer plays the game. For example, if a player, opens up the settings mid game, the state “pause” will be fired and the game will update its state to pause, everything will halt. The state handler will decide on what the next course of action is to be executed, given a state. Another use case of this in the developer world is the Redux state management library which keeps track of the application states in order to reduce the passing down of props into nested components.

If I could add on about the disadvantages, it is that it might increases the number of objects in the application. This might be an issue if there was limited memory. Although most of the time the application would have sufficient memory to run the whole program, there are cases where the memory limit might be exceed especially if each object of the strategy pattern is huge in memory size.

Overall, I think it is a well written post and I have learnt a lot, thanks for sharing Junhua!

--

--