Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Best way of accessing methods from far away objects

I'm currently working on my first larger java project (a game) and I'm having a bit of an organisation issue already with only a few (~40) classes.
I'm organizing my game something like this:

Project Organisation Model

That seems quite organized to me as every smaller class is categorized in a larger class. Obviously classes like the ObjectHandler or the Model will contain more classes, otherwise they'd be useless.

Now my issue is: When I'm trying to access the current PlayerSettings from the DynamicObjects in the GUI (for example when I'm trying to get the position of the player in order to draw it on the canvas), I'd have to create a long path like this:

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

(For that purpose I set most parameters public. I could also make them accessible with get()-methods, but that seems even more unorganized to me)

My question is - does that look any normal or are there any better ways to solve this organisation issue?

like image 648
Tobias Baumeister Avatar asked Sep 29 '22 01:09

Tobias Baumeister


2 Answers

The short answer is that - no, you wouldn't want a situation where you have to de-reference six objects in order to get to the data you need.

This probably means something is wrong with how you broke up your data and functionality into classes. You would generally want to group together objects that have to talk to each other.

One practical advice is that your objects do not have to form a tree. It may make sense for the DynamicObjects to hold a reference to the PlayerSettings. This is just an example: I don't know what make sense in your application, I am just trying to point out that you don't have to think of object relationships as a tree.

And regarding get methods vs. public members: it's a Java convention to use private variables with getters and setters. It has to do with maintainability and changing the implementation in the feature. Even if that doesn't convince you, I think it's a good thing to acquire the style standards that are common in Java coding.

like image 68
daphshez Avatar answered Oct 05 '22 22:10

daphshez


As the Law of Demeter suggests, having your dependencies loosely coupled and as ignorant of other classes as possible, helps to make your application more maintainable. Reducing the coupling between the components enables you to rapidly refactor or change individual modules/classes.

Your GUI shouldn't know where the player settings object comes from, nor should there be a reason for the GUI to fetch the information itself -- instead, the object should be injected to the GUI. There are many frameworks that enable Dependency injection for you, such as Google Guice, but you can also implement a simplistic version yourself by simply providing the object as parameter to the GUI, into the constructor or to a specific initialization method.

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

Also, try to organize the code into meaningful packages and reduce the visibility of the classes within a package so that they cannot be accessed from "far away". Only expose a public API outside the package for others to use and see. The same applies for methods and fields within classes; you should not use public fields (aside from constants). Expose as little information outside as possible and you will be able to refactor the code within you class (or package) without breaking code that's "far away".

like image 33
Mick Mnemonic Avatar answered Oct 06 '22 00:10

Mick Mnemonic