Skip to content

Instantly share code, notes, and snippets.

@chrismoutray
Last active March 29, 2018 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismoutray/f5024e4aebf44fcdd187270cd40109fb to your computer and use it in GitHub Desktop.
Save chrismoutray/f5024e4aebf44fcdd187270cd40109fb to your computer and use it in GitHub Desktop.
Example on using class inheritance to abolishing Switch-Case statements on a type

basic example of removing switch statement and instead using OO to have implementations that represent each type

so we don't do this!

public class Entity
{
    public string Type { get; set; }

    public int GetNewValueBasedOnType(int newValue)
    {
        int returnValue;
        switch (Type)
        {
            case "Type0":
                returnValue = newValue;
                break;
            case "Type1":
                returnValue = newValue * 2;
                break;
            case "Type2":
                returnValue = newValue * 3;
                break;
        }

        return newValue;
    }
}

and implement code like this!

public enum EntityType
{
    Type0 = 0,
    Type1 = 1,
    Type2 = 2
}

public abstract class Entity
{
    public abstract int GetNewValue(int newValue);
}

public class Type0Entity : Entity
{
    public override int GetNewValue(int newValue)
    {
        return newValue;
    }
}

public class Type1Entity : Entity
{
    public override int GetNewValue(int newValue)
    {
        return 2*newValue;
    }
}

public class Type2Entity : Entity
{
    public override int GetNewValue(int newValue)
    {
        return 3*newValue;
    }
}

here is another example from Vladimir Khorikov (http://enterprisecraftsmanship.com/)

instead of using switch on a licensing type implementations of Movice represent the types

important to note in the above example the GetNewValue did calculations in the child class

but below, the child classes just return values for a function in the abstract class

public abstract class Movie : Entity
{
    public virtual string Name { get; protected set; }
    protected virtual LicensingModel LicensingModel { get; set; }

    public abstract ExpirationDate GetExpirationDate();

    public virtual Dollars CalculatePrice(CustomerStatus status)
    {
        decimal modifier = 1 - status.GetDiscount();
        return GetBasePrice() * modifier;
    }

    protected abstract Dollars GetBasePrice();
}

public class TwoDaysMovie : Movie
{
    public override ExpirationDate GetExpirationDate()
    {
        return (ExpirationDate)DateTime.UtcNow.AddDays(2);
    }

    protected override Dollars GetBasePrice()
    {
        return Dollars.Of(4);
    }
}

public class LifeLongMovie : Movie
{
    public override ExpirationDate GetExpirationDate()
    {
        return ExpirationDate.Infinite;
    }

    protected override Dollars GetBasePrice()
    {
        return Dollars.Of(8);
    }
}

also see this for c# pattern matching

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment