Objective: The objective of this post is to give you an idea about Strategy Pattern, How to implement and why should you use in unity. Overview: Strategy pattern can be categorized as behavioral pattern, which is very simple and powerful. It can be considered in a kind of scenario where your object would perform a common action and that too selecting from various options available. What is Strategy Pattern? Strategy pattern defines a family of algorithms, encapsulates each one of them and makes them interchangeable. 1) Family of Algorithms- The definition says that the pattern defines the family of algorithms- it means we have functionality (in these algorithms) which will do the same common thing for our object, but in different ways. 2) Encapsulate each one of them- The pattern would force you to place your algorithms in different classes (encapsulate them). Doing so would help us in selecting the appropriate algorithm for our object. 3) Make them interchangeable- The beauty with strategy pattern is we can select at run time which algorithm we should apply to our object and can replace them with one another Seriously we will confuse with the definition at first time. So let’s make it simple. We can say it other simple words, Strategy pattern are algorithms inside a class which can be interchanged depending on the class used. This pattern is useful when you want to decide on runtime which algorithm to be used. Problem Statement: So consider a game scenario. A Military Tank can able to shoot the enemies with weapons consider as Bullet, Missile, grenade and bomb etc. How will plan to develop this scenario. Lets discuss one by one. In game development we will get new type of feature and requirements at any point of time. So, our responsibility is to make our code to be expandable at time. How a beginner behaves – Myself on earlier days: Without knowing the proper use of OOPS for given problem statement, a beginner will start with creating a class named as PlayerScript with list of transform for Weapons and fire bullets with the weapon selected. So this is how our normal script looks and it will work as expected. After few days a new requirement is coming up with adding more weapon types. What we are going to do now? Add a new weapon into prefab list and modify the weapon type. This will get more complex when weapon coming up with more properties like distance, moving speed and etc. This solution is not going to work anymore. Can we use inheritance? We can create a class for weapon and create sub classes for weapons. Will it work out? Yes, it will work out. Create a weapon class and extend with different weapon type classes. But, consider if you want to add a new method to set color for Military Tank. For this answer, you must create another Player class and its sub classes. So, Even this solution is not going to work anymore. Answer – Use Strategy pattern In this solution, create a weapon class and two more classes which extend this class. (I.e) Weapon and Color class. Weapon class is implemented by weapon type and Color class is implemented by color type. Here we got the solution. Let’s try with code. Strategy pattern - using in unity: Before you start consider that Cube as Military tank, Sphere as Bullet and Cylinder as Missile. Set Scene
Here is weapon manager class, Code Editor
And Color Manager for cube, Finally here comes PlayerScript, Finally you can happily see the strategy pattern was implemented successfully in unity. GamePlay: Find the attached unity package below. If you have any queries related this, please do comment. :) Next will meet all with Observer Pattern in Unity. Thank you.
0 Comments
|
AuthorSriram M G. ArchivesCategories |