Linked Questions

72 votes
5 answers

When is Singleton appropriate? [duplicate]

There is a widely accepted opinion that Singleton is an anti-pattern. As usual, there are always exceptions to the rule. Can you explain why Singleton is a bad choice in general and give an example of ...
Fishtoaster's user avatar
  • 25.9k
38 votes
3 answers

When to use a Singleton and when to use a static class [duplicate]

I've searched about this here and on StackOverflow and found some differences between the two. But I'm still not sure in what cases one would prefer a Singleton, and in what cases one would choose to ...
Aviv Cohn's user avatar
  • 21.4k
26 votes
7 answers

The Singleton Pattern [duplicate]

Possible Duplicate: When is Singleton appropriate? I am a new programmer (4 months into my first job) and have recently taken an interest in design patterns. One that I have used recently is the ...
Darren Young's user avatar
  • 2,175
31 votes
5 answers

What is the difference between all-static-methods and applying a singleton pattern? [duplicate]

I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton ...
shahensha's user avatar
  • 599
5 votes
5 answers

How do I deal with global variables in existing legacy code (or, what's better, global hell or pattern hell)?

So... We have this fairly complex project (~10k LOC, but there's duplicated code so it's hard to tell) with hundreds of global variables. The project has more dependencies on other projects, and many ...
ArthurChamz's user avatar
5 votes
2 answers

What alternatives to a singleton are there for a class which only can have one instance?

I need to represent an abstraction over various parts of the hardware for a game. I'm trying to decouple the code that does things like manage the logic of the game from the code that is API/platform ...
user112513312's user avatar
4 votes
2 answers

What's wrong with using a Singleton?

I'm working on a Python application in which there are two Singleton classes: App and Configuration. The former seems straight forward, only ever instantiate one App instance; the latter seems ...
pstatix's user avatar
  • 1,047
5 votes
2 answers

What is a relation between Dependency Injection, single instance, and singletons?

I read So Singletons are bad, then what? which was a great explanation of dependency injection as the solution for inversion of control. The asker assumed singleton and single instance were synonyms, ...
user3148156's user avatar
1 vote
3 answers

Why does dependency injection encourage collaboration to be exposed via constructors?

The general approach to DI that I see in answers like So Singletons are bad, then what? encourages business objects that collaborate with other objects to (a) not directly create those instances and (...
Daniel Davidson's user avatar
4 votes
2 answers

A module which is used by most other modules

I am pursuing object oriented design. So we don't want to use globals but yet sometime we have a module that most other modules use. This means we have to just pass it to all other modules and this ...
zadane's user avatar
  • 397
2 votes
4 answers

Do more object declarations affect the program?

I am programming in Windows Forms and MySQL. If I declare this in the program, I can use the connection and command objects in the whole .cs page: MySqlConnection connection = null; MySqlCommand ...
Sagotharan's user avatar
1 vote
2 answers

Is singleton the right way to go in the case of a game?

I've been reading the top posts of stackoverflow and SE and all over the place it says how bad singletons are but I am unsure how to rewrite my code. As of now I have two projects that bind into a ...
user3488765's user avatar
0 votes
2 answers

Why session should be a param when you write a query function?

I read a lot of examples where I read code like this: def get_user_by_id(session, id) ... and the function that calls that function needs to create or get the DB session a pass to the function. the ...
Tlaloc-ES's user avatar
  • 387
0 votes
1 answer

Why use the Singleton pattern over class functions and fields? [duplicate]

I'm going to start by saying that I understand that programming in mostly class functions and variables can be harmful to object-orientation, and that most of the time an instance is preferred. I'll ...
Ky -'s user avatar
  • 545
2 votes
0 answers

Convert singleton to single instance with concrete Go example

I have been using singletons in the past. However, reading articles like Singletons are Pathological Liars have led me to explore alternatives to singletons. There are a few posts discussion ...
peaxol's user avatar
  • 121