Viagra pills In my few days of being a developer, viagra pills I’ve heard and seen lots of talk about Singletons. Things like
Viagra pills Singletons are teh suck. Don’t use them. EVAR.
Viagra pills to
Viagra pills Singleton’s saved my life and marriage!
Viagra pills In fact, viagra pills I might’ve been the person that said both of these… gasp!
Viagra pills I never like to speak in absolutes, viagra pills so I’ll say it this way (see what I did there?)… if you’re striving to implement a singleton, viagra pills you’re probably wrong. For all you guys that are right, viagra pills I guess you’re right, viagra pills and you can leave now, viagra pills I guess. This might help you out.
Viagra pills I was reading a blog from Mr. Viagra pills Dependency where he was talking about statics and basically what amounts to global state, viagra pills and how that binds you into globally available, viagra pills global data, viagra pills which usually (always?) turns into anarchy.
Viagra pills How do we always get ourselves into this mess if we know it’s bad in the first place? Well, viagra pills some of us (those who are now zombocomming, viagra pills from above) don’t know it’s bad in the first place, viagra pills and the rest of us probably do it because it’s easy to implement, viagra pills and we understand it. Long story short, viagra pills we can do the singleton implementation in our sleep, viagra pills so we pull out the singleton hammer, viagra pills and smack the nail. It always goes in a little sideways, viagra pills but at least it goes in.
Viagra pills Maybe we should be using a different hammer?
Viagra pills What I’d like to do now, viagra pills is propose to split singleton in two. I think there are two distinct responsibilities that the current thought of ‘singleton’ introduces. Viagra pills It:
- Manages Session Scoping/Lifecycle/Lifestyle (albeit, viagra pills poorly, viagra pills only one strategy is available)
- Disables the ability to create more
Viagra pills When I talk about this out loud, viagra pills I call the two approaches the singleton implementation pattern, viagra pills and singleton lifecycle.
Viagra pills What I am a fan of, viagra pills is the singleton lifecycle. You create one of those objects, viagra pills and use it throughout the application. You don’t get messy and allow global access to it all over the place (which necessarily happens when you have global state). You intentionally inject it to collaborators, viagra pills and intentionally don’t inject it to non-collaborators (don’t just hand this guy out willy-nilly, viagra pills he’s important).
Viagra pills So naturally, viagra pills the naysayers ask/demand:
Viagra pills How do you manage that there is a public constructor on this class!? Dev’s are going to be new’ing this thing up all over the place!
Viagra pills I say:
Viagra pills False. Use a DI tool and let it manage it for you. Or establish a convention with your team, viagra pills and enforce it through social contracts. Viagra pills
Viagra pills Both of these approaches allow your scope lifecycle turn into whatever you want at any time, viagra pills in case you decide global isn’t what you really want. It can be application session scoped, viagra pills or some other scope, viagra pills test scoped, viagra pills or whatever you want… incredibly flexible.
Viagra pills If you go with the DI tool approach, viagra pills instead of tying yourself to a particular scope, viagra pills you can now have any scope you want, viagra pills by changing a configuration parameter. Delaying decisions until the latest responsible moment… seems like I’ve heard that one before.