Why Use Delegates?

Why do we need to use delegates instead of just calling functions directly?

Delegates solve a fundamental problem in software design: how to allow objects to communicate without creating tight coupling between them. Let's explore why this matters with a practical example.

Consider a game where we want to update the UI when a player takes damage. The direct approach might look like this:

class Player {
public:
  void TakeDamage(int Damage) {
    Health -= Damage;
    UserInterface->UpdateHealthBar(Health);
  }
private:
  UserInterface* UserInterface;
  int Health{100};
};

This creates several problems:

  • Our Player class now needs to know about UI implementation details
  • We can't easily change which UI elements respond to damage
  • Testing becomes harder because we can't separate player logic from UI logic
  • If we want to add new responses to damage (like sound effects), we need to modify the Player class

Using delegates solves these problems by inverting the dependency:

class Player {
 public:
  using DamageDelegate = std::function<
    void(int NewHealth)>;
  void SetOnDamageDelegate(DamageDelegate D) {
    OnDamageDelegate = D;
  }

  void TakeDamage(int Damage) {
    Health -= Damage;
    if (OnDamageDelegate) {
      OnDamageDelegate(Health);  
    }
  }

 private:
  DamageDelegate OnDamageDelegate;
  int Health{100};
};

Now:

  • Player doesn't need to know anything about UI
  • We can add or remove damage responses without touching the Player class
  • Different parts of our program can respond to damage in different ways
  • Testing is easier because we can provide test delegates

This pattern is particularly valuable in game development where systems often need to be loosely coupled but still communicate effectively.

Delegates and the Observer Pattern

An overview of the options we have for building flexible notification systems between game components

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Delegate Performance Impact
How does the performance of delegates compare to direct function calls?
Handling Multiple Event Types
Can I have an observer observe multiple different types of events from the same subject?
Pausing Observer Notifications
Is it possible to temporarily pause notifications to specific observers?
std::function vs Function Pointer Performance
What's the overhead of using std::function compared to raw function pointers?
Smart Pointers with Observers
Can I use smart pointers to automatically manage observer registration/unregistration?
Observer Pattern Without Dynamic Allocation
Is it possible to implement the observer pattern without dynamic memory allocation?
Or Ask your Own Question
Purchase the course to ask your own questions