vendredi 26 juillet 2019

Call implemented interface methods, but distinguish their calls on certain objects

I'm facing a design issue where I'm trying to call all objects that implement a certain interface IInitialize that has a method "Initialize".

"IInitialize" can implement several classes like GameManager, Player, Enemy etc.

However IInitialize can be also implemented in other classes that don't have to Initialize themselves in that exact moment when I want to let's say restart the game, so I need some sort of "categorization" of Init calls so other classes don't get called when I don't need them to be.

So here I have few options like creating another empty subclass of "IInitialize" and just make a call to "Initialize" through this type. All classes that Implement subclass will get this call. (it works, but it is a correct approach - not code smell?).

Another option is to create a generic "Initialize" interface and just pass the string or something similar to distinguish between calls.

So to recap: I implement the interface to various classes, let's say "ILife" which has a method "Die" to implement in the class. However I don't want all objects of type to die at a certain time, just the required ones.

Any other ideas or I'm going in the wrong direction?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire