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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:13:11+00:00 2026-05-29T08:13:11+00:00

From here: http://www.codingwithoutcomments.com/ If you do use Singletons, try to use dependency injection instead

  • 0

From here: http://www.codingwithoutcomments.com/

If you do use Singletons, try to use dependency injection instead of
calling getInstance() from the constructor, use this:

public MyConstructor(Singleton singleton) 
{
     this.singleton = singleton; 
} 

rather than this:

public MyConstructor() 
{
     this.singleton = Singleton.getInstance(); 
}

At the very least, using dependency injection allows you to do some unit testing of the class by adhering to good encapsulation principles.

How is this “dependency injection”? What is the meaning of dependency injection?
Doesn’t this defeat the purpose of the Singleton pattern?

Is this supposed to be used for some time and then removed later on?

  • 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-29T08:13:12+00:00Added an answer on May 29, 2026 at 8:13 am

    How is this “dependency injection”?

    “Dependency injection” means that you explicitly provide an object with references to all other objects that it depends on; that’s what the first example does when it passes a reference to the constructor.

    The alternative is to implicitly provide the objects that it depends on, by making them globally accessible. That’s what the second example does.

    The main advantage of dependency injection is that the dependency can be on an abstract interface; there’s no need to tie your class to a particular concrete class, or to a particular instance of that class. This makes testing more convenient – your class can be tested in isolation, interacting with “stub” implementations of any interface that it depends on. It also makes it easier to keep track of the dependencies, since they are all explicitly stated in the code.

    The main advantages of using globals are that they allow you to write slightly less code, and you don’t need to worry about managing dependencies until they become a problem; when they do, you can simply abandon the project and start a new one.

    Doesn’t this defeat the purpose of the Singleton pattern?

    That rather depends on what you think the purpose of the anti-pattern is. It certainly does remove the convenience of a globally-accessible instance; but, assuming the Singleton class does follow the anti-pattern, you still have the assurance that the object you receive is the One True Instance.

    Is this supposed to be used for some time and then removed later on?

    The singleton should be replaced by a normal class, passed by reference to its dependents, once you need to do things that the anti-pattern prevents (unit testing, multiple instances, subtypes, an abstract interface, etc.). There is little need to remove dependency injection once it’s there; you’ll only have to put it back again once you find that singletons don’t meet your requirements.

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

Sidebar

Related Questions

I have applied ie PNG from here: http://www.twinhelix.com/css/iepngfix/ So I can use transparent PNG
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
Having installed canvas from here http://www.rforge.net/canvas/files/ I try to plot: > canvas('test.js') > qplot(rnorm(100),
Using PDO with prepared statements (I learned from here: http://www.kitebird.com/articles/php-pdo.html ) I'm using a
The php code of barcode generator (which take from here http://www.barcodephp.com/en/userguide ) looks like
I basically try to reproduce the Socket example from here: http://www.silverlightshow.net/items/Sockets-and-their-implementation-in-SL2-Beta-1-including-a-chat-like-example.aspx I only made
Taken from here: http://www.cplusplus.com/doc/tutorial/inheritance/ What is inherited from the base class? In principle, a
I'm learning Databinding from here http://www.akadia.com/services/dotnet_databinding.html I'm just updating the dataset and then updating

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.