Is there an appropriate way to inject a service into singleton? <h2>Update</h2> <p>I will leave this question for now since there may or may not be a valid use case for something like this. For my case this was a design problem and instead of using a Singleton, I found a natural location to create an instance of a plain object and pass a reference to it were ever it is needed. Since it's now a plain object I can use constructor injection again. </p> <hr> <h2>Original question</h2> <p>I have what I think is an appropriate use of a Singleton. It's an object that will lazily load the images for folders and filetypes for use in a <a href="http://jruby.org" rel="nofollow noreferrer">JRuby</a>, <a href="https://www.eclipse.org/swt" rel="nofollow noreferrer">SWT</a> application.</p> <p>The class will look something like below. The methods need an instance of Display to create the images.</p> <p>The question is, what's the appropriate to inject the Display object and why? I was thinking of using a <code>begin ... rescue</code> block to set the value of @display to nil if the Display is not available and then providing a setter for unit testing. </p> <p>I always wonder if I am doing something wrong when I think I need a Singleton, even more so when I need to do something unusual with a Singleton, so I would not rule other other options.</p> <pre><code>require "singleton" class FileSystemIcons include Singleton attr_accessor :display, :cached_folder_image, :cached_file_images def initialize @display = Display.display @cached_folder_image = nil @cached_file_images = {} end def folder_image unless cached_folder_image ... self.cached_folder_image = converted_image end cached_folder_image end def file_image file_name ... unless cached_file_images[ext] ... cached_file_images[ext] end end end </code></pre>