Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mimic this java enum code in c#

Tags:

java

c#

enums

Is it possible to copy this functionality of offering an abstract method in an enum that inner enum constants must override and provide functionality to?

public enum Logic {
    PAY_DAY {
        @Override
        public void acceptPlayer(Player player) {
            // Perform logic
        }
    },
    COLLECT_CASH {
        @Override
        public void acceptPlayer(Player player) {
            // Perform logic
        }
    }
    ,
    ETC_ETC {
        @Override
        public void acceptPlayer(Player player) {
            // Perform logic
        }
    };

    public abstract void acceptPlayer(Player player);
}

If it can't:: Could you provide a way which I can implement lots of specific logic in a similar manner?

Edit :: I know that enums in C# are not really 'objects' like they are in Java, but I wish to perform similar logic.

Edit :: To clarify, I do not want to provide concrete classes for each specific bit of logic. IE, creating an interface acceptPlayer and creating many new classes is not appropiate

like image 965
AlanFoster Avatar asked Jan 31 '12 20:01

AlanFoster


1 Answers

Here's one option - not using enums, but something similar-ish...

public abstract class Logic
{
    public static readonly Logic PayDay = new PayDayImpl();
    public static readonly Logic CollectCash = new CollectCashImpl();
    public static readonly Logic EtcEtc = new EtcEtcImpl();

    // Prevent other classes from subclassing
    private Logic() {}

    public abstract void AcceptPlayer(Player player);

    private class PayDayImpl : Logic
    {
        public override void AcceptPlayer(Player player)
        {
            // Perform logic
        }
    }

    private class CollectCashImpl : Logic
    {
        public override void AcceptPlayer(Player player)
        {
            // Perform logic
        }
    }

    private class EtcEtcImpl : Logic
    {
        public override void AcceptPlayer(Player player)
        {
            // Perform logic
        }
    }
}

You say that you don't want to provide a concrete class for each bit of logic - but that's basically what you'd be doing in Java anyway, it's just that the class would be slightly hidden from you.

Here's an alternative approach using delegates for the varying behaviour:

public sealed class Logic
{
    public static readonly Logic PayDay = new Logic(PayDayAccept);
    public static readonly Logic CollectCash = new Logic(CollectCashAccept);
    public static readonly Logic EtcEtc = new Logic(player => {
        // An alternative using lambdas...
    });

    private readonly Action<Player> accept;

    private Logic(Action<Player> accept)
    {
        this.accept = accept;
    }

    public void AcceptPlayer(Player player)
    {
        accept(player);
    }

    private static void PayDayAccept(Player player)
    {
        // Logic here
    }

    private static void CollectCashAccept(Player player)
    {
        // Logic here
    }
}

In both cases, you still get a fixed set of values - but you won't be able to switch on them. You could potentially have a separate "real" enum, but that would be a bit messy.

like image 60
Jon Skeet Avatar answered Oct 07 '22 17:10

Jon Skeet