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.

   // Coaching  strategy demo

   #include <iostream>

   // Base class for all coaching strategies:
   class CoachingStrategy
   {
       public:
           CoachingStrategy() { };
           virtual ~CoachingStrategy() { };

           // Pure abstract method:
           // When a coach needs to tell the team what needs to happen, this method is used:
           virtual void BroadcastStrategy() = 0;

   };

   // Coaching strategy implementations:

   // When the team needs to be on the attack, use this:
   class OffensiveStrategy : public CoachingStrategy
   {
       public:
           OffensiveStrategy() : CoachingStrategy() { };
           void BroadcastStrategy() override
           {
               std::cout << "Crash the net!" << std::endl;
           }
   };

   // When the team needs to defend the lead:
   class DefendLeadStrategy : public CoachingStrategy
   {
       public:
           DefendLeadStrategy() : CoachingStrategy() { };
           void BroadcastStrategy() override
           {
               std::cout << "Don't be sloppy, maintain this lead!" << std::endl;
           }
   };


   // The team bench where the coach gives recommendations based on the current game stats.
   class TeamBench
   {
       public:
           // Constructor:
           TeamBench()
           : teamScore(0), opposingScore(0)
           {
               // Default strategy:
               this->pOffensiveStrategy = new OffensiveStrategy();
               this->pDefensiveStrategy = new DefendLeadStrategy();
               this->SetStrategy(this->pOffensiveStrategy);
           };

           // Destructor:
           ~TeamBench()
           {
               // Clean up:
               this->pCurrentStrategy = nullptr;
               delete this->pOffensiveStrategy;
               delete this->pDefensiveStrategy;
           }

           // When our team scores, call this method:
           void TeamScored()
           {
               teamScore++;

               if(teamScore > opposingScore)
               {
                   this->SetStrategy(this->pDefensiveStrategy);
               }
           }

           // When the opposing team scores, call this method:
           void OpposingTeamScored()
           {
               opposingScore++;

               if(opposingScore >= teamScore)
               {
                   this->SetStrategy(this->pOffensiveStrategy);
               }
           }

           // Get the coaches reccomended strategy:
           void BroadcastCoachStrategy()
           {
               this->pCurrentStrategy->BroadcastStrategy();
           }


       protected:

           // Protected helper to set the current strategy.
           void SetStrategy(CoachingStrategy* pStrategy)
           {
               this->pCurrentStrategy = pStrategy;
           }

           // Create the various strategies from the get-go to avoid allocations:
           CoachingStrategy* pOffensiveStrategy;
           CoachingStrategy* pDefensiveStrategy;

           // The current strategy:
           CoachingStrategy* pCurrentStrategy;

           // Game stats:
           int teamScore;
           int opposingScore;
   };

Find any bugs in the code? let us know!