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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:50:01+00:00 2026-05-24T23:50:01+00:00

I recently read that singleton is an anti-pattern and should not be used unless

  • 0

I recently read that singleton is an anti-pattern and should not be used unless it is really needed.

In all our projects, we use the singleton pattern to hold some cache data. For example:

class SomeClass
{
   public SomeClass()
   {
       var somedata = Singleton.Instance.GetSomeData(stringRepresintation); // or like that
       var someData = Singleton.Instance.SomeData;
   }      
}

What is the recommended design to hold that data (static class, or something else)?

  • 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-24T23:50:02+00:00Added an answer on May 24, 2026 at 11:50 pm

    Well, you could think of the cache as a dependency – and pass an instance (probably the same instance) into everything which needed it. That’s what I’d do in a situation where I was already using dependency injection.

    That would make it easier to test those classes which need the cache – you could create a new cache for each test, and not have to worry about clearing out an existing one. You could also parallelize tests without worrying about two tests messing up each others caches. Additionally, it makes the cache dependency clearer.

    (Whether or not you use an interface to represent the cache as cdhowie suggests is up to you. You don’t have to, although it would decouple the classes from their dependencies more if you did. If your cache is very simple and you don’t mind using the production implementation in tests, it may not be worth extracting an interface.)

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

Sidebar

Related Questions

I recently read that in StringWriter and StringReader are used for writing and reading
I recently read somewhere (I am really sorry I can't provide the source) that
I recently read that making a class singleton makes it impossible to mock the
I recently read that the content provider content://sms shouldn't be used to access the
I've recently read that embedding ruby inside JavaScript is not a good idea. However,
Possible Duplicate: PHP closing tag I've read recently that the ?> should be omitted
I recently read that getters/setters are evil and I have to say it makes
I recently read somewhere that writing a regexp to match an email address, taking
I recently read in a presentation on Scribd that Facebook had benchmarked a variety
I recently read through Code Complete, and it recommends that I create a project

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.