Is injection dependency an anti-template chain?

Here's the problem, let's say we make a video game and want to use Injection Dependency. Here is what we have:

Game Class  // This is just the code to keep track of the overall game logic
Character Class // This would be the guys in the game, good and bad guys both
Weapon Class // The weapons for the characters

So usually, when I inject dependencies, I would enter the current location on the maps and the state of the game in the character, so that my character has information, to know where to create it, etc. Then I have a character creating a weapon and entering values ​​in relation to the strength of the weapon, as well as some other general game states from Game Class, etc.

It almost looks like an anti-pattern to me. I say, because now you have (at least, it seems to me that way) code that is very fragile and difficult to change. If we want to change the information about the state of the game, which is transmitted, we are forced to change all three classes. We make the initial change to the Game class, then modify the Character, and then finally modify the Weapon class. This is a lot of work, especially if you go to 5 levels, not 3. Although yes, this would make it easier to test devices than without DI.

This sounds like bad practice again. Is it usually done? What was our “MotherShip” model where everything is on the top level. Therefore, instead of playing a game, a character creates a weapon, we allow the game (or another class) to create all of them.

, , . , .

+3
2

, , , , . , . . , , , , , .

, , , , Factory. , . , . , 3 4 , , Factory .

+2

- ( tster, ), , , - , .

, , . IGame Character Game.

, , . , .

+5

Source: https://habr.com/ru/post/1723841/


All Articles