|
When I only have one of something that I need to share across a multi-project app, I use a "global static" (class). Trying to maintain references, parameters and avoiding cycles otherwise is (for me) extra time spent for nothing.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I know it's pedantic, but the right tool for the job. I'll grant you that the singleton pattern can be overused, especially by programmers who aren't proficient at OOP principles like encapsulation and data hiding. "But I need this value over here too, and I don't know how to make that happen unless I let everybody see it." Globals, globals everywhere.
The most common use I make of static ly-declared 'singleton' values is where there is one instance of a thing but multiple users. In this case, the users are implemented as instances of the class around the static value. The instances then mediate access to the static value by the class user.
I know what I'm doing... I'm a professional.
Software Zen: delete this;
|
|
|
|
|
I built this so I can easily layer that very thing on top of what I have but I don't think it will be necessary.
Real programmers use butterflies
|
|
|
|
|
That makes sense though.
There is a time and a place for singletons. I'm not denying that. I just don't like them. They "smell" to me.
They do solve some very sticky design problems though, particularly when you're creating binary interfaces to be used across DLL/EXE/NETWORK and you need a "factory" to create objects from. That's probably where I've used them the most, because you can't export static "interfaces" with binary interface binding like that. (Think COM or DCOM, or really any kind of binary RPC)
Real programmers use butterflies
|
|
|
|
|
I don't like singleton.. but I have nothing against static method (aka functions)..
So I am halfway there!
|
|
|
|
|
So you and I are on the same page there.
Real programmers use butterflies
|
|
|
|
|
Nice!
I had a brainwave. I suspect the popularity of singleton classes goes hand in hand with unit testing with mocks. Another thing I dislike, though I would sometimes concede them the title of necessary evil.
Anyway, you can't swap a static class since the code is directly accessed by consumer. But if the class is passed as a constructor argument to consumer, then you can swap it for something else there...
|
|
|
|
|
Super Lloyd wrote: hand in hand with unit testing with mocks. Another thing I dislike, though I would sometimes concede them the title of necessary evil.
Yet another opinion we share. I don't do unit testing unless I have to. For my non-commercial code, I don't really write that much in the way of tests until I have to. I know that's evil of me, but there it is. I figure I'm just challenging myself to write more stable code the first time. Yeah, that's it. *whistles*
I can wrap the static methods with a class that just delegates to them, which is what I was referring to when I said "ersatz 'singleton'" - I just don't like it. I prefer the statics, but even that I don't like.
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: I have an aversion to creating things like statically backed ersatz "singleton" classes in languages that support them.
One of my previous managers was talking: I send programmer to C++ design course, after such course he writes only singletons.
Yep, for C++ programmer, the simplest way to look smart is to write a singleton.
|
|
|
|
|
"If you learn nothing else, learn singleton" -- and they learn nothing else.
|
|
|
|
|
I'm forced to write either a static class (like what I have now) or an ersatz singleton backed by that static class (wrapping it)
I'm sticking with the all static class.
I can't afford the this pointer to create a real singleton for the kind of things I'm doing. Right now all the arguments to calls are being put in registers, but I only have so many of those, and this is all critical code paths - anything I do impacts the final framerate of the emulator.
I have to be very careful in my design here. I can't get fluffy. I can't afford it.
Real programmers use butterflies
|
|
|
|
|
Here they are translated to English, using a flip chart: Bec Hill translates "Non Je Ne Regrette Rien" (Edith Piaf) - YouTube[^]
May be slightly NSFW depending on your Internet Police, so watch at home if you are not sure.
Made me laugh anyway!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
She must be the daughter of Benny Hill!
|
|
|
|
|
Not really, being French meself!
|
|
|
|
|
Q. What do you call someone who speaks three languages?
A. Trilingual
Q. What do you call someone who speaks two languages?
A. Bilingual
Q. What do you call someone who speaks one language?
A. English
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
There is a woman who hangs around the play-ground offering cheap batteries.
She sells 'C' cells by the seesaw.
The less you need, the more you have.
Even a blind squirrel gets a nut...occasionally.
JaxCoder.com
|
|
|
|
|
You're fired! Here's your coat.
|
|
|
|
|
Please explain the joke.
I do a bit o' English, but this beyond me. And it's a spade, not a shovel.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
It is an English "tongue twister", i.e. a phrase that gets difficult to say multiple times, especially at speed. Ofte used as a children's game. The original phrase is "She sells sea-shells by the sea shore".
|
|
|
|
|
Thanks. And good exercise.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Good point ... I know about "she sells..." from my English lessons some time ago, and now I also know the meaning of "seesaw". And... both tongue-twisters can even be combined in one sentence ...
|
|
|
|
|
No, Bob.
Just sign the divorce papers and you can both move on with your lives.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Heh,
Haven't you heard? Marriage Is forever!
Best Wishes,
-David Delaune
|
|
|
|
|
I'm told duct tape can fix anything
Real programmers use butterflies
|
|
|
|
|
Wendy was never that into Bob to begin with. (Nor the other way around I suppose.)
|
|
|
|