Java: the best way to access methods from remote objects

I am currently working on my first major java project (game), and I already have a problem with organizing with only a few (~ 40) classes.
I organize my game like this:

Project organization model

This seems pretty organized to me, as every smaller class is classified in a larger class. Obviously, classes such as ObjectHandler or Model would contain more classes, otherwise they would be useless.

Now my problem is: when I try to access the current PlayerSettings from DynamicObjects in the GUI (for example, when I try to get the playerโ€™s position to draw it on the canvas), I would have to create a long path like this:

 int x = gui.engine.model.objHandler.player.playerSettings.getX(); 

(For this purpose, I set most of the public parameters. I could also make them available using get() methods, but to me it seems even more disorganized)

My question is: does this look normal or are there any ways to solve this problem?

+5
source share
3 answers

Short answer: no, you do not need a situation where you need to remove six objects to get the data you need.

This probably means that something is wrong with how you split your data and functions into classes. As a rule, you would like to group objects that should talk to each other.

One practical tip: your objects should not form a tree. For DynamicObjects, it might make sense to store a link to PlayerSettings. This is just an example: I donโ€™t know what makes sense in your application, Iโ€™m just trying to indicate that you do not need to think of object relations as a tree.

And regarding get and public methods: this is a Java convention for using private variables with getters and setters. This is due to maintainability and a change in implementation in this function. Even if this does not convince you, I find it good to acquire style standards that are common in Java coding.

+2
source

As stated in the Law of Demeter , if your dependencies are freely connected and as little as possible aware of other classes, it helps to make your application more convenient for maintenance. Reducing the connection between components allows you to quickly reorganize or change individual modules / classes.

Your GUI should not have information about where the player settings object came from, and there should be no reason for the GUI to get this information - instead, the object should be entered into the GUI. There are many frameworks that allow "Dependency Influences" for you, such as Google Guice , but you can also implement the simplified version by simply providing the object as a GUI parameter in the constructor or a specific initialization method.

 public class MyGUI { private PlayerSettings settings; //... public void initialize(PlayerSettings settings) { this.settings = settings; } //... } 

In addition, try organizing the code into meaningful packages and reduce the visibility of the classes inside the package so that they cannot be accessed from the โ€œdistantโ€ one. Expose the public API outside the package so that others can use and see. The same applies to methods and fields within classes; you should not use public fields (other than constants). Expose as little information as possible outside, and you can reorganize the code inside your class (or package) without breaking the code, which is "far".

+2
source

A quick way to clean mailboxes, such as using the Google Guava EventBus. You can use it to publish events, and then everything that subscribes to these events will act on them, all without any components that know each other or where the events come from. Its a quick way to effectively separate and manage dependencies.

If you want something even more elegant, take a look at reactive programming and explore RxJava.

0
source

All Articles