The main problem that I encounter quite often, but have ever found a clean solution, is where you want to code the behavior for the interaction between different objects of a common base class or interface. To make this a little concrete, I will give an example:
Bob coded a strategy game that supports "cool geographic effects." They are rounded to simple limits, for example, if troops go in the water, they slow down by 25%. If they walk along the grass, they slow down by 5%, and if they walk along the sidewalk, they slow down by 0%.
Now the leadership told Bob that they needed new types of troops. There will be jeeps, boats, as well as hovercraft. In addition, they wanted the jeeps to do damage if they went into the water, and all three types of terrain were ignored by the hovercraft. Rumor has it that they can add another type of terrain with even more features than unit slowdown and damage.
The following is an example of very crude pseudo-codes:
public interface ITerrain
{
void AffectUnit(IUnit unit);
}
public class Water : ITerrain
{
public void AffectUnit(IUnit unit)
{
if (unit is HoverCraft)
{
}
if (unit is FootSoldier)
{
unit.SpeedMultiplier = 0.75f;
}
if (unit is Jeep)
{
unit.SpeedMultiplier = 0.70f;
unit.Health -= 5.0f;
}
if (unit is Boat)
{
}
}
}
public class Grass : ITerrain
{
public void AffectUnit(IUnit unit)
{
if (unit is HoverCraft)
{
}
if (unit is FootSoldier)
{
unit.SpeedMultiplier = 0.95f;
}
if (unit is Jeep)
{
unit.SpeedMultiplier = 0.85f;
}
if (unit is Boat)
{
unit.SpeedMultiplier = 0.0f;
unit.Health = 0.0f;
Boat boat = unit as Boat;
boat.DamagePropeller();
}
}
}
, , - . , . , , , , , . , , IUnit.
, , . , , , . , . , , . .
, , , . 100 IUnit ITerrain, .
. , ?