javaooadlaw-of-demeter

"Bridging" (Connecting) methods between distant classes


Lets suppose I have Planet and Star classes and GameLogic class in the following way

//Planet.java
package Game;

class Planet //invisible outside of package
{
   public String getType() {return "Im a Planet";}
}

.

//Star.java
package Game;

class Star //invisible outside of package
{
   public String getType() {return "Im a Star";}
}

.

//GameLogic.java
package Game;

public class GameLogic //notice the PUBLIC keyword
{
   //arrayList of stars
   //arrayList of planets

   //a ton of methods for the logic of the game itself
}

These all belong to the Game package which treats only the whole logic of the game with no input/output.

Then, in a different package called UI I have the Text class (UI.Text) which controls user input, prints output and has direct access to the GameLogic because the game can only be played by creating an instance of the GameLogic class.

//Text.java
package UI;
import Game.*;

public class Text
{
   //some control vars

   //MAIN method
   //input and output methods
   //bla bla bla
}

So the issue is: The game will be instantiated in Text.java by creating a GameLogic instance. Given this, if i wanted to output the getType() functions from Star and Planet, I'd have to complete the "road" that connects Text with Planet/Star in the following way

//GameLogic.java
package Game;

public class GameLogic
{
   //same as before

   public String getPlanetType(int n) {return planetArrayList.get(n).getType();}
   public String getStarType(int n) {return starArrayList.get(n).getType();}
}

In short, the getType() methods already exist in a "too-far-away" class, so it forces me to create these simple, yet repetitive, "bridge" methods to connect both ends.

TL-DR version:

class A has getType()

class B has array of A

class C needs to access getType() from array of A in B 

I believe this is a very inefficient and time consuming method and that there is a better alternative (possibly involving Interfaces?), so if anyone could provide me with some help, I'd be very grateful.

Thank you


Solution

  • Don't believe anything regarding performance.

    The only thing that matters is reality.

    And in reality, the JIT will most often turn your attempts to micro-optimize upside down.

    Meaning: don't worry about performance (too much) until you see real issues. If you have issues, start measuring instead of making assumptions.

    Instead: focus on good, clean designs; follow the SOLID principles; and write clean, readable, maintainable code. That will pay of a hundred times more than worrying about such subtleties.

    ( and just to give you an example: a method getType() that returns a String already looks like a broken design. Considered using enums? Or even more strange: do you realize that alone by using different classes, your objects have different types?)

    And if you still think you should look into performance, than start by reading about the JIT, how it works, what it does, and so on.