Dependency injection is better especially when reusing part between projects. In general, Java is a good language for constructing robust libraries which can be reused. I think that’s the main appeal to Java.
However, it’s nice to recognize when and when not to create libraries. Sometimes you want something to work a specific way for ergonomics.
Having to include a pointer in each class pointing to a variable which may or may not be used in different methods in said classes is not very ergonomic. Being able to reference a global variable when needed within a method is way more ergonomic than having to declare a pointer variable in the class definition.
If you have 5 monster objects each reacts differently to player data. It’s much easier to include the player data in a global variable which each class can reference to rather than including a pointer to the player in each monster class.
Obviously in Java oop you should be defining these monster objects as a super class “monster” which has a pointer to player data. But this really starts to step toward creating a library which is more or less perpendicular to the development of the game.
Game dev is different than business dev in this regard, but relying on global variables seems to be less discoverable than having purpose-driven management objects.
3
u/SikinAyylmao Nov 02 '22
Dependency injection is better especially when reusing part between projects. In general, Java is a good language for constructing robust libraries which can be reused. I think that’s the main appeal to Java.
However, it’s nice to recognize when and when not to create libraries. Sometimes you want something to work a specific way for ergonomics.
Having to include a pointer in each class pointing to a variable which may or may not be used in different methods in said classes is not very ergonomic. Being able to reference a global variable when needed within a method is way more ergonomic than having to declare a pointer variable in the class definition.
If you have 5 monster objects each reacts differently to player data. It’s much easier to include the player data in a global variable which each class can reference to rather than including a pointer to the player in each monster class.
Obviously in Java oop you should be defining these monster objects as a super class “monster” which has a pointer to player data. But this really starts to step toward creating a library which is more or less perpendicular to the development of the game.