For a personal project, I'm working on a small web-based game.
I have a Card
class that has a Status
property, and there are case statements all over the place. I thought, hey, this is a great oppurtunity for Replace Conditional with Polymorphism!
The problem is, I have a couple methods that do stuff like this:
public class Card
{
public void ChangeStatus()
{
switch (Status)
{
case MyStatusEnum.Normal:
Status = MyStatusEnum.Underwater;
break;
case MyStatusEnum.Underwater:
Status = MyStatusEnum.Dead;
break;
// etc...
}
}
}
When refactoring it in the new NormalCard
class, I'm overriding the ChangeStatus
method like this:
public override void ChangeStatus()
{
base.Status = MyStatusEnum.Underwater;
}
The problem is this object of NormalCard
has a status of Underwater
. I can't reassign the type of this
, and I don't really want to change the return of the methods from void
to CardBase
. What options do I have? Is there a standard way of doing this?
Edit Tormod set me straight. I want the State Pattern. Thanks all!