October 29, 2020
design-pattern

Strategy Pattern

Strategy Pattern is very interesting and easy design pattern. This pattern is generally used to change the algorithm or the common behavior of an object at runtime.

Think of an action game where a warrior has to fight a lot of villains. At the beginning of the game the fighter has 100% power and health condition. At the beginning the fighter is in an aggressive mood. Gradually the warrior’s health began to decline and he goes into defensive mood. Then he starts looking for food, when he gets food his health becomes 100% again and he goes into aggressive mood again.

For this consider the following Fighter Class.

public class Fighter
 {
        public int Health { get; set; }
        public void ChangeMood(String mood)
        {
            switch (mood)
            {
                case "Aggressive":
                    SetAnggsiveBehavior();
                    break;
                case "Defensive":
                    SetDefensiveBehavior();
                    break;
            }
        }
        private void SetDefensiveBehavior()
        {
            Console.WriteLine("Defensive Mood");
        }
        private void SetAnggsiveBehavior()
        {
            Console.WriteLine("Aggressive Mood");
        }
 }
It’s corresponding client code is:
class Program
 {
        static void Main(string[] args)
        {
            var fighter = new Fighter();
            var random = new Random();
 
            fighter.Health = random.Next(1, 100);
 
            if (fighter.Health <= 50)
            {
                fighter.ChangeMood("Defensive");
            }
            else if (fighter.Health > 50)
            {
                fighter.ChangeMood("Aggressive");
            } 
            Console.ReadKey();
        }
 }

Here, if the fighter’s health is above 50, we take the fighter to aggressive mode and if it is below 50, we take him to defensive mode.

But if we give the class library of this code to someone else and he wants to create a new mood for the fighter, he can’t. We have to give him the full source code of our Fighter class. Then our object oriented programming will be the violation of OCP (Open Close Principle). OCP means a class or entity will only be open for extension but closed for modification.

It is also a violation of the Principle of Least Knowledge rule. Because the Fighter class has to know all the moods all the time. Here Fighter class don’t need this knowledge all time. We can tell him at runtime, you fight in this mood now.

Since the common feature of all fighters is to fight, we can create a common interface IFighter with a method called Fight ().

public interface IFighter
 {
     void Fight();
 }

In real life out methods can be big. That’s why it is better to convert them into class

public class Aggresive : IFighter
 {
    public void Fight()
    {
        Console.WriteLine("Fighter is now in aggresive mood");
    }
 }
public class Defensive : IFighter
 {
        public void Fight()
        {
            Console.WriteLine("Fighter is now in defensie mood");
        }
 }
public class Fighter
 {
        public int Health { get; set; }
        private IFighter _fighter;
        public void ChangeMood(IFighter fighter)
        {
            _fighter = fighter;
            _fighter.Fight();
        }
 }
class Program
 {
        static void Main(string[] args)
        {
            var fighter = new Fighter();
            var random = new Random();
 
            fighter.Health = random.Next(1, 100);
 
            if (fighter.Health <= 50)
            {
                fighter.ChangeMood(new Defensive());
            }
            else if (fighter.Health > 50)
            {
                fighter.ChangeMood(new Aggresive());
            } 
            Console.ReadKey();
        }
}

Now if we give the class library of this design to someone else and if he wants to create a new mood for the fighter, he will never have to modify the Fighter class. He needs to create a class for the new mode that will implement IFighter.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

One thought on “Strategy Pattern

Leave a Reply

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