I'm adapting Image Downloader from Google Android blog. I want ImageDownloader
to be singleton since I'll be using it in multiple places in my application. I want also to be able to manipulate Bitmaps
using different Strategies
(eg. produce transparent bitmaps).
Context:
I want to be able to use ImageDownloader
in one activity and set transparent bitmaps, and in another use the same ImageDownloader
but get black&white bitmaps using different strategy object.
You think you do, but you don't want ImageDownloader to be a Singleton. Singleton is very much overused, and not appropriate in your case. Think about it: how can you manipulate Bitmaps using different strategies if there is only one instance of the class doing the manipulating?
What you want is the ability to create instances of ImageDownloader via static methods, which you can do without making it a Singleton. These methods are called Factory methods, and there are many good web pages describing them.
You probably want something like:
class ImageDownloader {
static ImageDownloader createImageDownloader(Strategy s) {...}
//...
}
Each call to the method with the same argument could return the same instance of ImageDownloader, provided the instances don't store state. Some versions of this approach are referred to as "Multiton". Google will tell you more.