简体   繁体   English

如何用策略实现单例?

[英]How to implement singleton with strategies?

I'm adapting Image Downloader from Google Android blog. 我正在调整Google Android博客中的Image Downloader I want ImageDownloader to be singleton since I'll be using it in multiple places in my application. 我希望ImageDownloader是单例,因为我将在我的应用程序中的多个位置使用它。 I want also to be able to manipulate Bitmaps using different Strategies (eg. produce transparent bitmaps). 我还希望能够使用不同的Strategies来操作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. 我希望能够在一个活动中使用ImageDownloader并设置透明位图,而在另一个活动中使用相同的ImageDownloader但使用不同的策略对象获取黑白位图。

You think you do, but you don't want ImageDownloader to be a Singleton. 你认为你这样做,但你不希望ImageDownloader成为Singleton。 Singleton is very much overused, and not appropriate in your case. Singleton非常过度使用,在您的情况下不合适。 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. 你想要的是能够通过静态方法创建ImageDownloader的实例,你可以在不使它成为单例的情况下完成。 These methods are called Factory methods, and there are many good web pages describing them. 这些方法称为Factory方法,并且有许多描述它们的好网页。

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. 每次调用具有相同参数的方法都可以返回相同的ImageDownloader实例,前提是实例不存储状态。 Some versions of this approach are referred to as "Multiton". 这种方法的某些版本被称为“Multiton”。 Google will tell you more. 谷歌会告诉你更多。

I'm more inclined to agree with DJClayworth answer, but to answer your question, the best way to implement the singleton pattern is to use an enum: 我更倾向于同意DJClayworth的回答,但回答你的问题,实现单例模式的最佳方法是使用枚举:

public enum ImageDownloaderWrapper
{
    INSTANCE;

    public static final ImageDownloader IMAGE_DOWNLOADER;

    private ImageDownloaderWrapper()
    {
        IMAGE_DOWNLOADER = new ImageDownloader();//this is where you would initialize it... looks like it has a default constructor
    }
}

To get a hold of the instance: 要获取实例:

ImageDownloaderWrapper.INSTANCE.IMAGE_DOWNLOADER.download(...

You can also take advantage of static imports: 您还可以利用静态导入:

import static some.package.structure.ImageDownloaderWrapper.INSTANCE;

Then it's a bit simpler: 然后它有点简单:

INSTANCE.IMAGE_DOWNLOADER.download(...

To account for different strategies I guess you'd have to extend ImageDownloader and add the appropriate logic for dealing with strategies in that subclass (the type of IMAGE_DOWNLOADER should also correspond to the subclass you created). 为了考虑不同的策略,我猜你必须扩展ImageDownloader并添加适当的逻辑来处理该子类中的策略(IMAGE_DOWNLOADER的类型也应该对应于你创建的子类)。

You could pass a strategy as parameter to the methods responsible for the image downloading/manipulation. 您可以将策略作为参数传递给负责图像下载/操作的方法。

Then the strategy passed will handle the manipulation. 然后通过的策略将处理操作。 It's a fairly ugly hack though. 虽然这是一个相当丑陋的黑客。 See DJClayworth's answer for the more clean code ideas. 请参阅DJClayworth的答案,了解更清晰的代码创意。

If you want to have compelte idea of Singleton pattern and how it can be implement, refer this article 如果您想了解Singleton模式以及如何实现它,请参阅本文

http://www.codinguide.com/2010/04/singleton-pattern.html http://www.codinguide.com/2010/04/singleton-pattern.html

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM