If class A has a unique interaction with each of classes B, C and D, should the code for the interaction be in A or in B, C and D?
I am writing a little game whereby lots of objects can have unique interactions with other objects. For example, an EMP
hits a sentry gun
and disables it. It could also hit a grenade
and detonate it, it could also hit a player
and applying a slowing effect.
My question is, should this code go in the EMP class, or be spread amongst all the other classes? My gut tells me to do this polymorphically so that I simply tell each class to deal with the EMP strike however it likes, which would allow me to add more items and change how they deal with the EMP without changing the EMP code.
However, the EMP only currently interacts with about 4 out of 50 of my objects, so filling 46 objects with empty RespondToEMP() calls seems wrong. It also seems somewhat unintuitive, if I decide to remove the EMP, I need to change every other class, and the EMP class itself ends up fairly tiny. It also means if I want to change how the EMP behaves I need to go looking through all the different classes to find all the possible usages. Additionally, if the EMP has some generic effects such as an explosion, this effect would definitely be inside the EMP code, away from all the other effects which would be distributed.