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!
You seem to misunderstand the word “internal”. That document is more telling you that you should not do for example:
but more so
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.