lundi 4 décembre 2017

Singleton vs Static Class (C++)

I've recently been reading through Bob Nystrom's book on Game Programming Patterns, but I've gotten a bit stuck on Singletons: http://ift.tt/19Vd4Jk.

This is the part just before the "What We Can Do Instead" section.

"The usual argument for choosing singletons over static classes is that if you decide to change the static class into a non-static one later, you’ll need to fix every call site."

I agree that if you change a static class to a non-static one (e.g. a Singleton), you'll need to change every call site.

"In theory, you don’t have to do that with singletons because you could be passing the instance around and calling it like a normal instance method. "

As opposed to what? Is this not how a Singleton works? What don't you have to do with Singletons?

"In practice, I’ve never seen it work that way. Everyone just does Foo::instance().bar() in one line."

I'm not seeing the problem. Wouldn't this code work with a Singleton, and still work if you make the instance static (and there by create a static class)?

"If we changed Foo to not be a singleton, we’d still have to touch every call site."

I don't understand why. What does a "not Singleton" refer to here?

"Given that, I’d rather have a simpler class and a simpler syntax to call into it."

Can someone help me understand how he arrived at his conclusion?

Aucun commentaire:

Enregistrer un commentaire