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 6914167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:18:45+00:00 2026-05-27T09:18:45+00:00

In almost every project I create a few classes which implement Singleton pattern. For

  • 0

In almost every project I create a few classes which implement Singleton pattern. For example, data manager – if there’s some work with file system, data loader – if an application connects to the internet, different resources managers, etc. Sometimes there are up to 5-7 such classes and I start feeling that I’m doing something wrong. Is it a bad practice to use Singleton pattern too much?

  • 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-27T09:18:46+00:00Added an answer on May 27, 2026 at 9:18 am

    Singletons are not necessarily problems — having a single object that is an expert in its domain can be very useful — but explicitly coding the singleton-ness into the object is almost always a bad idea (and frequently is done badly too). It turns out that it is better to leave the configuration of the application — including the decision of what classes have singleton instantiations — to a separate layer that specializes in that, such as Spring for Java. Like that, the management layer can look after what is a singleton, what is a singleton within a particular context (e.g., within the scope of a session) and what always needs to be manufactured anew. That leaves you free to focus on writing the business logic.

    For an example of why Singletons can be problems and why they should be singletons by management/configuration, consider a class that manages the connection to a database. Classic case for a singleton you might say. You’d be right too, right up until you find that your app has grown to the point where it has to integrate connections to two databases (it happens!) and then you’ve got to disentangle the whole mess. If you’ve kept your code agnostic of whether it is dealing with singletons or not, you stand an excellent chance of being able to handle it all by just reconfiguring; some classes will be connected to one DB and some to the other, but they’ll just get on with it with minimal fuss. (Anything that needs both… well, that’s why I said “excellent chance”.)

    Another example of why Singletons can be problems comes when you’re writing tests for your code. (You do write tests, yes?) Explicit singletons are very difficult to test, as they’re hard to configure and hard to isolate. You can’t tear them down properly between tests because that implies having many of them. If your app uses singletons only by configuration, a testing configuration can easily change that and you can do your tests far more easily.

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

Sidebar

Related Questions

Almost every project that I work will have some sort of tab delimited file
Duplicate post, see: When do you use the "this" keyword? On almost every project
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
well for some strange reason IE gives me and InvalidAuthenticityToken error almost every time
In almost every project, I can't decide on how to deal with certain global
I have a file in a Subversion repository which changes almost every time it's
In my java project, almost every non-static method I've written is synchronized . I've
I wrote some classes that I use with many different projects. For example, I
Almost every Java book I read talks about using the interface as a way
Almost every Python web framework has a simple server that runs a wsgi application

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.