Good habits for game development

Started by
34 comments, last by Serapth 11 years, 2 months ago
There's nothing about deferred allocation that requires a single instance or global access. You can have deferred allocation with a class that you can have multiple instances of. You can have deferred allocation with local or member variables.
Advertisement

There's nothing about deferred allocation that requires a single instance or global access. You can have deferred allocation with a class that you can have multiple instances of. You can have deferred allocation with local or member variables.

Yes, but it is a trait of the singleton. I am not saying there aren't other ways to accomplish the same thing, but it is one thing a singleton gives you.

Yes, but it is a trait of the singleton.

No, it's not. Singleton is two things: single instance and global access. That's the definition. If you have those two things it's a singleton. If you don't then it's not. Some common implementations have deferred allocation, but you don't need a singleton to do it and a singleton doesn't require it. If you want deferred allocation there's no reason to go and restrict your deferred allocated class to a single instance and force it to have global access, which is exactly what you are saying to do if you're saying reach for a singleton when you need deferred allocation. Deferred allocation is orthogonal to both number of instances a class can have and scope of access. That's saying every time you need to use new (a deferred creation mechanism) you should have a singleton.

That's saying every time you need to use new (a deferred creation mechanism) you should have a singleton.

No, its not. Allocation and access are coupled with a singleton. If new was smart enough to not new an existing object, I would agree with you.

I am also not saying that if you want deferred allocation, use a singleton. I am saying a singleton provides that trait, nothing more. So, if you have a global object, that requires a single instance and you want to defer it's allocation until first access, a singleton provides this. I am not saying other data structures don't, nor that there aren't other ways to accomplish this.

Allocation and access are coupled with a singleton.

Not part of the singleton definition. Allocation can happen before a singleton is first accessed and it would still be a singleton. And you can couple allocation and access for an object without bringing in either global access or single instances of the class.

I am also not saying that if you want deferred allocation, use a singleton.

Yes, you did! You added deferred allocation to jbadams list of times when you need a singleton.

I am saying a singleton provides that trait, nothing more.

No, it doesn't! A singleton is defined by two traits: single instance of a class and global access. Common implementation often have deferred allocation, but it's not a requirement. Ex: a Meyer's singleton can be constructed before first use.

So, if you have a global object, that requires a single instance and you want to defer it's allocation until first access, a singleton provides this.

Which is saying that if you want a singleton with deferred allocation, then you want any old singleton! You're confusing a common implementation detail of a pattern for part of that pattern.

I am also not saying that if you want deferred allocation, use a singleton.

Yes, you did! You added deferred allocation to jbadams list of times when you need a singleton.

Well that would be the crux of the disagreement between us. I read jbadams post as times you could use a singleton, not when you need one. In re-reading his post, I will admit outright, this was a mistake on my behalf.

This topic is closed to new replies.

Advertisement