I don’t know if singletons are the way to go, and certainly aren’t the simplest way, but I am using singletons myself. I handle this similar to a way minecraft handles blocks. However, unlike minecraft, I use two classes; an Entity class, which holds information, and an Abstract Controller class, which does the logic.
The abstract Controller has a few static variables/functions. Here is the basic code:
private static Controller[] controllers = new Controller[128];
public static Controller getController(int id) {
if(id<0 || id>controllers.length) return null;
return controllers[id];
}
This is where minecraft stops, and where minecraft gets block/item ids (assuming you’ve played enough minecraft to know that).
I take it a bit further. To increase compatibility/readability, I include a static HashMap:
private static HashMap<String, Integer> names = new HashMap<String, Integer>();
public static Controller getController(String name) {
if(names.containsKey(name)) return getController(names.get(name));
return null;
}
Next, we need to make a few controllers, but first they need a way to be added to the list. So in our abstract class, add in the following constructor/variables:
public final int id;
public final String name;
public Controller(String name) {
for(int index=0; index<controllers.length; index++) {
if(controllers[index]==null) {
controllers[index] = controller;
names.put(name, index);
id = index;
this.name = name;
}
}
}
Now, anytime we make one of our singleton controllers, it will be kept in our array of controllers. The last things to do are make something for the controllers to act on, and make the controllers act on it. I handled this by first making an entity class that has its controllers id:
public class Entity {
public int controllerId;
}
Then I made an update function in the Controller class:
public void update(Entity entity) {
//do stuff
}
And an update function in the Entity class:
public void update() {
Controller.getController(controllerId).update(this);
}
And that is about all for basic singleton Controllers. Of course, you will have to add in more, like an x and y to the Entity class, but I think you can figure that out yourself. 