Singletons

What I meant doesn’t actually contain the word “static” anywhere in the code. You just make your factory an object, use instance methods, and by all means allow for more than one factory. If you store an instance somewhere on an existing “natural singleton”, such as your “App” object, then it’s already a de facto singleton, and it turns out that that’s pretty much all you need.

Ah, I wasn’t sure about the Class portion of what you said. It’s just something I’ve never run into.

if I try to to summary this,

  1. make sure you only type once “new Factory()” in your project
  2. pass the instance around to every states and entities

I think a few of you would benefit greatly from having a browse through Effective Java (2nd Edition) by Joshua Bloch.

Right. Chances are you’ll want to stick it on a field somewhere. When I’m using spring, I call it “context”. My scala apps using the cake pattern call it “module”.

You pass it where it’s needed, usually just for other singletons that need to create other instances they depend on via the factory. At low levels where you’re creating instances that are private to lower levels of your system, you can simply use inner classes. You’d only need to use the factory if you need to customize it independently through the factory. Your fine-grained classes like game entities, sprites, and so forth should not be aware of your factory.