HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Triggering methods that change instance state on other objects using a Dictionary<string, Action>

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
objectsdictionarymethodsinstanceactionthatstateusingtriggeringother

Problem

I've been reading up on Actions and delegates for the last couple of days, and I think I'm finally starting to understand, but I wanted to check that I was using them correctly, or at least using them in a way that isn't totally insane.

I'm using a text-based game (as such) as a platform for learning about what I suspect is called event-oriented programming. For a really terrible example, imagine the player has a torch that can set things on fire. I don't want the torch to do target.ChangeState(States.OnFire), that's giving the torch way too much power over other objects - nothing is preventing an action like nonFlammableRock.ChangeState(States.OnFire) I COULD have the torch tell the game manager entity that it wants to set something on fire, and then let the game manager decide if it's allowed to do so... but instead, I came up with this:

TestObject

internal class TestObject
{
    private string instanceVar;
    private Dictionary eventHandlers;

    public TestObject(string newInstanceVar)
    {
        this.instanceVar = newInstanceVar;
        eventHandlers = new Dictionary();
        eventHandlers.Add("event0", PrintInstanceVar);
        eventHandlers.Add("event1", InstanceVarToUpper);
        eventHandlers.Add("event2", ChangeOwnInstanceVar);
    }

    public void PrintInstanceVar()
    {
        Console.WriteLine(instanceVar);
    }

    public void InstanceVarToUpper()
    {
        instanceVar = instanceVar.ToUpper();
    }

    public void Event(string eventName)
    {
        eventHandlers[eventName].Invoke();
    }

    public void ChangeOwnInstanceVar()
    {
        instanceVar = "Object has changed its instance variable";
    }
}


Program

private static void Main(string[] args)
    {
        var test1 = new TestObject("First test obejct");

        test1.Event("event2");
        test1.Event("event0");
        test1.Event("event1");
        test1.Event("event0");

        Console.ReadKey();
    }


In short, I want to tell an o

Solution

This looks ok to me on first glance.

I can only suggest the use of an Enum to strongly type the events that you are registering.

This allows you to search the code base quickly and avoids any typo's you may make:

private string instanceVar;
    private Dictionary eventHandlers;

    public TestObject(string newInstanceVar)
    {
        this.instanceVar = newInstanceVar;
        eventHandlers = new Dictionary();
        eventHandlers.Add(Events.event0, PrintInstanceVar);
        eventHandlers.Add(Events.event1, InstanceVarToUpper);
        eventHandlers.Add(Events.event2, ChangeOwnInstanceVar);
    }

    public enum Events
    {
        event0,
        event1,
        event2
    }

Code Snippets

private string instanceVar;
    private Dictionary<string, Action> eventHandlers;

    public TestObject(string newInstanceVar)
    {
        this.instanceVar = newInstanceVar;
        eventHandlers = new Dictionary<Events, Action>();
        eventHandlers.Add(Events.event0, PrintInstanceVar);
        eventHandlers.Add(Events.event1, InstanceVarToUpper);
        eventHandlers.Add(Events.event2, ChangeOwnInstanceVar);
    }

    public enum Events
    {
        event0,
        event1,
        event2
    }

Context

StackExchange Code Review Q#128569, answer score: 2

Revisions (0)

No revisions yet.