You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
}