Strategy Pattern could be very fascinating and straightforward design sample. This sample is usually used to alter the algorithm or the frequent conduct of an object at runtime.

Think of an motion sport the place a warrior has to battle numerous villains. At the start of the sport the fighter has 100% energy and well being situation. At the start the fighter is in an aggressive temper. Gradually the warrior’s well being started to say no and he goes into defensive temper. Then he begins in search of meals, when he will get meals his well being turns into 100% once more and he goes into aggressive temper once more.

For this take into account the next Fighter Class.

public class Fighter
 {
        public int Health { get; set; }
        public void ChangeMood(String temper)
        {
            change (temper)
            {
                case "Aggressive":
                    SetAnggsiveBehavior();
                    break;
                case "Defensive":
                    SetDefensiveHabits();
                    break;
            }
        }
        non-public void SetDefensiveHabits()
        {
            Console.WriteLine("Defensive Mood");
        }
        non-public void SetAnggsiveBehavior()
        {
            Console.WriteLine("Aggressive Mood");
        }
 }

It’s corresponding consumer 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 well being is above 50, we take the fighter to aggressive mode and whether it is under 50, we take him to defensive mode.

But if we give the category library of this code to another person and he needs to create a brand new temper for the fighter, he can’t. We have to offer him the total supply code of our Fighter class. Then our object oriented programming would be the violation of OCP (Open Close Principle). OCP means a category or entity will solely be open for extension however closed for modification.

It can be a violation of the Principle of Least Knowledge rule. Because the Fighter class has to know all of the moods on a regular basis. Here Fighter class don’t want this information all time. We can inform him at runtime, you battle on this temper now.

Since the frequent function of all fighters is to battle, we are able to create a standard interface IFighter with a way referred to as Fight ().

public interface IFighter
 {
     void Fight();
 }

In actual life out strategies might be massive. That’s why it’s higher to transform them into class

public class Aggresive : IFighter
 {
    public void Fight()
    {
        Console.WriteLine("Fighter is now in aggresive temper");
    }
 }
public class Defensive : IFighter
 {
        public void Fight()
        {
            Console.WriteLine("Fighter is now in defensie temper");
        }
 }
public class Fighter
 {
        public int Health { get; set; }
        non-public 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 category library of this design to another person and if he needs to create a brand new temper for the fighter, he won’t ever have to switch the Fighter class. He must create a category for the brand new mode that can implement IFighter.

Source link