Is it bad practice for a class to pass itself to its own objects?
For example:
public class MainView extends SurfaceView {
Stack<GameObject> objs;
public MainView() {
new GameObject(this);
}
public void Add(GameObject obj) {
objs.push(obj)
}
public void Remove(GameObject obj) {
objs.pop(obj)
}
}
public class GameObject {
MainView parent;
int state;
public GameObject(MainView parent) {
this.parent = parent;
parent.Add(this);
}
public void doStuff() {
if(state == 1) {
new GameObject(parent);
}
else if(state == 2) {
parent.Remove(this);
}
}
}
I made an effort to remove static methods/variables. Now i have to pass the view to it’s own objects.
Is this a bad practice?
Should objects have logic or should the SurfaceView contain all the logic?
Are you developing a game? If so I’d suggest you have a read this and this. The surfaceview should only have the job of rendering your game objects to the screen. Each game object however should hold the logic (its behavior) and a reference to a canvas to draw to which will eventually be drawn to the surfaceview. The overall logic of the game should however be in a class (eg. named world) that takes care of all the generating and managing of all the game objects.
I hope this helps.