【发布时间】:2010-08-20 07:12:54
【问题描述】:
我正在开发一款游戏。视图、线程和引擎都完成了。现在我将讨论如何将坐标设置为我的位图。
我已经使用 getters/setters-method 成功地做到了这一点。我一直在网上看到大多数优秀的游戏开发者都说“让你的成员变量公开”之类的东西。
自从我阅读了 http://developer.android.com/guide/practices/design/performance.html 的避免内部 Getters/Setters 部分后,我开始想:如何更改我的 Coordinates 类以在没有“setters”的情况下实现这一目标?
现在我的坐标类看起来像:
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;
}
}
我应该如何更改我的代码来实现这一点?方法调用很昂贵,但我仍然不知道如何在没有 getter 和 setter 的情况下重构我当前的代码。
更新
public GameEngine getInstance(){
if(instance == null){
instance = new GameEngine(resources,view);
}
return instance;
}
更新 2
游戏引擎
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;
}
还有我的游戏视图
static GameEngine engine;
public GameView(Context localContext) {
//Other stuff
engine = GameEngine.getInstance(context.getResources(), this);
//Other stuff
}
提前致谢!
【问题讨论】: