Strategy Design Pattern Example

Strategy Design Pattern is used to manage multiple subclasses (each implements various behaviors and shares some common behaviors) via composition instead of inheritance. This design pattern encapsulates each algorithm to be used interchangeably during runtime depending on client usage.

Basic Coding

This is what I used to code in my previous works. It’s simple and gets the thing done. When there are more weapons added, this class will be modified again. This poses risks where changing a class after testing might break it. Also, if there is complex formula involved in calculating the output for each weapon, this Weapon Class will be very difficult to read with hundreds or thousands lines of codes.

Advance Coding

Here I breakdown the main code into Interfaces and Multiple Classes. It seems that it requires more effort and more lines of codes compared to the basic coding. However, in the long term, it is easier to maintain the code as each class only holds a small portion of code (easier to read and find). Advance coding applies polymorphism (object with same method but does different things) and adheres to the principal of “Open for Extension, Close for Modification”.


If I had to add more weapons and sound effects, basic coding becomes really messy and difficult to read with all of the if-statements conditions whereas for Advance Coding, it requires some time to properly manage these classes into folders.

The codes in Advance Coding section can be further simplified by implementing Factory Pattern which will be discussed in my subsequent post!

Quick Search: ,

Leave a Reply

Your email address will not be published. Required fields are marked *