Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:54:21+00:00 2026-05-25T05:54:21+00:00

I have spent the last couple of hours reading about the singleton pattern and

  • 0

I have spent the last couple of hours reading about the singleton pattern and why not to use it, amongst others those really good sites:

  • Singleton I love you, but you’re bringing me down
  • How to Think About the “new” Operator with Respect to Unit Testing
  • Where have all the Singletons Gone?

I guess quite a lot of you know these already.

Looking at my code after reading that, I clearly am one of the maybe 95% of programmers that misunderstood and misused the singleton pattern.
For some cases, I can clearly remove the pattern, but there are cases where I am unsure what to do:

I know singletons for logging are accepted, one reason for that being that information only flows into them but not back into the application (just into the log file or console etc of course).

What about other classes which do not meet that criteria but are required by a lot of classes?

For example, I have a settings object which is required by a lot of classes. By a lot, I mean more than 200.

I have read into some other SO questions like “Singletons: good design or a crutch?”, and all of them pointed out why using singletons is discouraged.
I understand the reasons for that, but I still have one major question:

How do I design a class which needs a single instance, accessible from everywhere, if not using the Singleton pattern?

The options I can think of would be:

  1. Use a static class instead (though I don’t see how this would be any better, looking at OOP and unit testing).
  2. Have it created in an ApplicationFactory and perform dependency injection on every single class that needs it (keep in mind it’s 200+ for some cases).
  3. Use a singleton anyway, as the global access bonus outweighs the disadvantages for that case.
  4. Something completely different.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:54:21+00:00Added an answer on May 25, 2026 at 5:54 am

    It will depend on exactly what you mean by a settings object.

    Do all 200 classes need all the settings; if not why do they have access to the unused settings?
    Where do the settings come from and is there a good reason why each class can’t load its settings as and when required?

    Most importantly though, don’t make changes to working code just because the code uses a pattern which is frowned upon. I’ve only used the singleton pattern once but I’d use it again.

    EDIT:
    I don’t know your constraints but I wouldn’t worry about multiple access from a file until it had been shown to be an issue. I would split up the configuration into different files for different classes/ groups of classes or, preferably, use a DB instead of files with different tables providing data for each class.

    As an aside I’ve noticed that once you put the data in a db people seem to stop worrying about accessing it multiple times even though you’re still going to the file system in the end.

    PS: If other options aren’t suitable I’d use a singleton… you want data to be globally available, you’re not willing to use dependency injection, you only want the file to be read once; you’ve limited your options and a singleton isn’t that bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent the last couple of days to use Core Plot for the
Urgh, I have spent the last couple of hours on this now. I normally
I have spent the last few hours putting togeather the following code after reading
I have spent last couple of hours searching for answers here but nothing seems
I have spent the last couple of hours looking through other questions on this
I have spent the last couple hours scouring the Internet for a solution to
I have spent the last couple of hours trying to get my sessions to
I have spent the last couple of hours battling to write some javascript and
I have spent the last 3 hours trying to get this code working. I
I'm starting to learn R and Sweave and have spent the last couple of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.