Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:02:18+00:00 2026-06-07T02:02:18+00:00

Imagine the following class hierarchy: interface IRules { void NotifyPickup(object pickedUp); void NotifyDeath(); void

  • 0

Imagine the following class hierarchy:

interface IRules
{
    void NotifyPickup(object pickedUp);
    void NotifyDeath();
    void NotifyDamage();
}

class CaptureTheFlag : IRules
{
    public void NotifyPickup(Pickup pickedUp)
    {
        if(pickedUp is Flag)
            GameOver();
    }

    public void NotifyDeath()
    {
    }

    public void NotifyDamage()
    {
    }
}

class DeathMatch : IRules
{
    public void NotifyPickup(Pickup pickedUp)
    {
        points++;
    }

    public void NotifyDeath()
    {
        lives--;
    }

    public void NotifyDamage()
    {
    }
}

class GameWorld
{
    IRules gameMode;

    public Main(IRules gameMode)
    {
        this.gameMode = gameMode;
    }

    object[] worldObjects;

    public void GameLoop()
    {
        foreach(object obj in worldObjects)
        {
            // This call may have a bunch of sideeffects, like getting a pickup
            // Or a player dying
            // Or damage being taken
            // Different game modes are interested in different events / statistics.
            obj.Update();


            // Stuff happens...
            gameMode.NotifyDamage();
            // Stuff happens...
            gameMode.NotifyDeath();
        }
    }
}

So here I’ve got an interface which contains Notify* functions. These are callbacks. Different game modes are interested in different events of the game. It’s not really possible to access the concrete objects creating these events because they’re buried in the worldObjects array. Imagine we are adding new game modes to our game. The IRules interface will get hugely bloated, containing all the possible things a game mode may be interested in, and most calls will be stubbed! How can I prevent this?

Edit 2: Concrete example

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:02:20+00:00Added an answer on June 7, 2026 at 2:02 am

    My final solution was the C# equivalent of what xtofl posted. I created a class which stored a bunch of delegates in it. These delegates started off with default values (so they would never be null) and the different concrete IRules classes could choose to overwrite them or not. This worked better than abstract or stubbed methods because it doesn’t clog the interface with unrelated methods.

    class GameEvents
    {
        public Action<Player> PlayerKilled = p => {};
        public Func<Entity, bool> EntityValid = e => true;
        public Action ItemPickedUp = () => {};
        public Action FlagPickedUp = () => {};
    }
    
    class IRules
    {
        GameEvents Events { get; }
    }
    
    class CaptureTheFlag : IRules
    {
        GameEvents events = new GameEvents();
        public GameEvents Events
        {
            get { return events; }
        }
    
        public CaptureTheFlag()
        {
            events.FlagPickedUp = FlagPickedUp;
        }
    
        public void FlagPickedUp()
        {
            score++;
        }
    }
    

    Each rule set can choose which events it wants to listen to. The game simply calls then by doing Rules.Events.ItemPickedUp();. It’s guaranteed never to be null.

    Thanks to xtofl for the idea!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following class that displays some sort of hierarchy: class BaseList2D(object): def __init__(self):
Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x
Imagine the following: class Repository { private ObservableCollection<ModelClass> _allEntries; public ObservableCollection<ModelClass> AllEntries { get
Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
Imagine the following situation: class IAlarm : public boost::enable_shared_from_this<IAlarm> { boost::shared_ptr<IAlarm> getThisPointerForIAlarm() { return
Imagine the following classes: class A { public string Test {get; set;} } class
Imagine the following VB.NET class: Public Class P Public Function GetDumbList() As List(Of DumbPeople)
Consider the following sample code: class MyClass { public long x; public void DoWork()
imagine you have the following interfaces: public interface IInterfaceA : IInterfaceX { // //
Imagine the following scenario: template<class T> void myFunction(T *) { //do nothing } void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.