Strategy Examples:

Java
Java
Python
Python
PHP
PHP
C#
C#
C++
C++
TypeScript
TypeScript
▸ Strategy Quick Review

Strategy Pattern in C#

To demonstrate the Strategy Pattern in C#, we'll put ourselves in the shoes of a hockey coach trying to get their team to the playoffs this year. When it comes to hockey, the team needs to play differently depending on the current state of the game. For instance, if your team scored a point and is up 6-0, we know that we could probably lay off the throttle a little, but if it's a close and exciting one-goal-game, both teams need to play at their best to take home the victory.

   using System;

   // The Base strategy interface:
   public interface CoachingStrategy
   {
       void BroadcastStrategy();
   }

   // When the team is tied or losing the game:
   public class OffensiveStrategy: CoachingStrategy
   {
       public void BroadcastStrategy()
       {
           Console.WriteLine("Crash the net!");
       }
   }

   // When the team is ahead of the other team:
   public class DefendLeadStrategy: CoachingStrategy
   {
       public void BroadcastStrategy()
       {
           Console.WriteLine("Don't be sloppy, maintain this lead!");
       }
   }


   // Class that utilizes the strategies:
   // Maintains strategies itself to avoid GC:
   public class TeamBench
   {
       protected CoachingStrategy offensiveStrategy;
       protected CoachingStrategy defensiveStrategy;

       protected CoachingStrategy currentStrategy;

       protected int teamScore;
       protected int opposingTeamScore;

       public TeamBench()
       {
           offensiveStrategy = new OffensiveStrategy();
           defensiveStrategy = new DefendLeadStrategy();
           this.SetStrategy(this.offensiveStrategy);

           teamScore = 0;
           opposingTeamScore = 0;
       }

       // Helper method to set the current strategy:
       protected void SetStrategy(CoachingStrategy strategy)
       {
           this.currentStrategy = strategy;
       }

       // When our team scores, compare the scores and set the strategy.
       public void TeamScored()
       {
           this.teamScore++;

           if(teamScore > opposingTeamScore)
           {
               this.SetStrategy(this.defensiveStrategy);
           }
       }

       // When the opposing team scores, check if a strategy change is needed.
       public void OpposingTeamScored()
       {
           this.opposingTeamScore++;

           if(opposingTeamScore >= teamScore)
           {
               this.SetStrategy(this.offensiveStrategy);
           }
       }

       // Get the recommended strategy from the coach:
       public void BroadcastCoachStrategy()
       {
           this.currentStrategy.BroadcastStrategy();
       }
   }

Find any bugs in the code? let us know!