tags:

views:

127

answers:

2

Hello! I working on a Game. The View, Thread and the Engine are done. Now I going into how to, for example, set Coordinates to my Bitmap.

I have successfully done that using getters/setters-method. I've been reading around the web there the most of the good game developers say that "make your member variable public" and all that stuff.

Since I read the Avoid Internal Getters/Setters section at http://developer.android.com/guide/practices/design/performance.html, I started to wonder: How I can change my Coordinates-class to achieve this without "setters" for example?

Now my Coordinates-class look like:

package com.mygame.mygame;

public class Coordinates {
    int x;
    int y;
    Coordinates instance = null;

 public Coordinates getInstance(){
  if(instance == null){
   instance = new Coordinates();  
  }
  return instance;
 }

    public Coordinates() {    
    }

    public int getX() {
        return x;
    }

    public void setX(int value) { 
        x = value;
    }

    public int getY() {
        return y;
    }
    public void setY(int value) {
        y = value;
    } 

}

How should I change my code to achieve this? Method calls are expensive, but I still have no idea how to restructure my current code without getters and setters.

UPDATED

 public GameEngine getInstance(){
 if(instance == null){
  instance = new GameEngine(resources,view);  
  }
  return instance;
  }

UPDATE 2

GameEngine

    static Resources res;

static GameView view;

static GameEngine instance = null;

public static GameEngine getInstance(Resources localResources, GameView localView){
    view = localView;
    res = localResources;
    if(instance == null){
        instance = new GameEngine(); //Init-stuff in the GameEngine
    }
    return instance;
}

and my GameView

static GameEngine engine;
public GameView(Context localContext) {
   //Other stuff
   engine = GameEngine.getInstance(context.getResources(), this);
   //Other stuff
}

Thanks in advance!

+1  A: 

You seem to misunderstand the word "internal". That document is more telling you that you should not do for example:

public Coordinates(int x, int y) {
    setX(x);
    setY(y);
}

but more so

public Coordinates(int x, int y) {
    this.x = x;
    this.y = y;
}

In other words, avoid using getters/setters in the same class as where they're definied.

Your current class isn't doing that anywhere by the way. Only the getInstance() method is just pretty pointless in this context. I'd get rid of it.

BalusC
Wow! That makes sense! But , is _this_ the way to do it? Aren't method calls expensive in these perspectives too? Well, I have the method calls in my Engine.
Julian Assange
It's the normal practice, yes. As to the expensiveness, getter/setter method calls are particularly cheap, it's more a matter of style. But in a machine with only a few resources and little to no JIT optimizations like mobile phones, I'd imagine that every optimization is more than welcome.
BalusC
Well okay, so that's my way to go then. I updated my code with a piece of code from my GameEngine. Is this Instance useless too?
Julian Assange
Because that `getInstance()` method isn't been declared `static`, I consider it still pointless. It doesn't prevent you from creating multiple instances. This smells too much like a halfbaked singleton. But that may also depend on the whole context.
BalusC
Any method call to set a scalar value is going to have overhead on the call stack, so just make all your members public. It breaks encapsulation, and the Java mentality of hiding members, but it makes for faster code.This example still shows a method call for assigning two variables. That's 2x as fast as the original setters, but still slow.See these talks for more info on developing games in java on android:http://www.youtube.com/watch?v=U4Bk5rmIpichttp://www.youtube.com/watch?v=7-62tRHLcHk
serotonin
@serotonin: That can all be true, but that's not what the linked document is telling and where this question is all about :)
BalusC
Okay, I'm gonna get this soon. In my View I initialize the engine by doing something like engine = new GameEngine(resources,view).If I then change the instance, the objects and the method to static, what and how could it be a bigger difference? Accepted also. :-)
Julian Assange
You could do that in the constructor of the `View` class, yes. There's no need to make the methods static. You just **use** the created instance.
BalusC
So, I think I understand now. I update my code as you see. I would be glad if you can answer if this is a correct and useful instance?
Julian Assange
So the Android VM cannot inline as well as HotSpot?
Thorbjørn Ravn Andersen
@Agnus: I don't see how `static` is useful in this context. @Thor: that's apparently the case as stated in the documentation the OP linked (and the movies of serotonin).
BalusC
As of 2.2 (Froyo), the Dalvik JIT doesn't do method inlining. This is expected to change in a future release.
fadden
+1  A: 

public is the default for members, unless otherwise specified. Just don't use the getter or setter for x and y.

serotonin
The default visibility is not public. As in the current code, the members are visible by classes in the same package only.
BalusC