BlackBerry:使我的图像线程类可用于所有实例

发布于 2024-12-23 16:50:06 字数 628 浏览 3 评论 0原文

我创建了一个图像线程类,它在加载图像的实例中运行更新方法:

public class ImageThread implements Runnable {
    private Bitmap image;
    private String url;
    private CustomEventField c;


    public ImageThread(String url, CustomEventField c){

        this.url = url;
        this.c = c;
    }

   public void notifyUs(){



        this.c.update(image);
    }

    public void run() {

        Bitmap img =  downloadImage.connectServerForImage(this.url); //data processing

        image = img;

        notifyUs();

    }




}

我在构造函数中发送上下文,但是我只能将其用于 CustomEventField。如果我想将此图像类用于其他类怎么办?我可以再制作一个该类的副本,但我想让事情井井有条。

I made a image thread class that runs the update method in the instance that is loading the image:

public class ImageThread implements Runnable {
    private Bitmap image;
    private String url;
    private CustomEventField c;


    public ImageThread(String url, CustomEventField c){

        this.url = url;
        this.c = c;
    }

   public void notifyUs(){



        this.c.update(image);
    }

    public void run() {

        Bitmap img =  downloadImage.connectServerForImage(this.url); //data processing

        image = img;

        notifyUs();

    }




}

I send the context in the constructor, however I can only use it for CustomEventField. What if I wanted to use this image class for other classes? I could just make another copy of this class, but I want to keep things organized.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪之魂 2024-12-30 16:50:07

定义一个单独的接口类型,然后所有类都可以根据需要实现该接口类型,例如:

ImageThreadCallback.java:

public interface ImageThreadCallback
{
    void update(Bitmap image);
}

ImageThread.java:

public class ImageThread implements Runnable
{
    private Bitmap image;
    private String url;
    private ImageThreadCallback c;

    public ImageThread(String url, ImageThreadCallback c)
    {
        this.url = url;
        this.c = c;
    }

    public void notifyUs()
    {
        this.c.update(this.image);
    }

    public void run()
    {
        this.image = downloadImage.connectServerForImage(this.url); //data processing
        notifyUs();
    }
}

CustomEventField.java:

public class CustomEventField implements ImageThreadCallback
{
    public void update(Bitmap image)
    {
        ...
    }
}

CustomEntryField.java:

public class CustomEntryField implements ImageThreadCallback
{
    public void update(Bitmap image)
    {
        ...
    }
}

Define a separate interface type that all of your classes can then implement as needed, eg:

ImageThreadCallback.java:

public interface ImageThreadCallback
{
    void update(Bitmap image);
}

ImageThread.java:

public class ImageThread implements Runnable
{
    private Bitmap image;
    private String url;
    private ImageThreadCallback c;

    public ImageThread(String url, ImageThreadCallback c)
    {
        this.url = url;
        this.c = c;
    }

    public void notifyUs()
    {
        this.c.update(this.image);
    }

    public void run()
    {
        this.image = downloadImage.connectServerForImage(this.url); //data processing
        notifyUs();
    }
}

CustomEventField.java:

public class CustomEventField implements ImageThreadCallback
{
    public void update(Bitmap image)
    {
        ...
    }
}

CustomEntryField.java:

public class CustomEntryField implements ImageThreadCallback
{
    public void update(Bitmap image)
    {
        ...
    }
}
¢好甜 2024-12-30 16:50:07

首先,ImageThread 可能应该扩展 Thread 或重命名为类似 ImageUpdateTask 的名称。

正如您在问题中提到的,它难以重用的原因是它耦合性太强。这可以通过将方法抽象为单独的接口来解决。

public interface Receiver<T> {
    public void pass(T t);
}

-

public class ImageThread extends Thread {
    private Bitmap image;
    private final String url;
    private final Receiver<Bitmap> updater;

    public ImageThread(String url, Receiver<Bitmap> updater) {
        this.url = url;
        this.updater = updater;
    }

    @Override
    public void run() {
        Bitmap img =  downloadImage.connectServerForImage(this.url);
        //data processing

        image = img;
        updater.pass(image);
    }
}

-

public class CustomEventFieldUpdater implements Receiver<Bitmap> {
    @Override
    public void pass(Bitmap image) {
    //TODO
    }
}

-

public class CustomEntryFieldUpdater implements Receiver<Bitmap> {
    @Override
    public void pass(Bitmap image) {
    //TODO
    }
}

Firstly, ImageThread should probably either extend Thread or be renamed to something like ImageUpdateTask.

As you mentioned in your question, the reason this is hard to reuse is because it is too strongly coupled. This can be solved by abstracting methods to separate interfaces.

public interface Receiver<T> {
    public void pass(T t);
}

-

public class ImageThread extends Thread {
    private Bitmap image;
    private final String url;
    private final Receiver<Bitmap> updater;

    public ImageThread(String url, Receiver<Bitmap> updater) {
        this.url = url;
        this.updater = updater;
    }

    @Override
    public void run() {
        Bitmap img =  downloadImage.connectServerForImage(this.url);
        //data processing

        image = img;
        updater.pass(image);
    }
}

-

public class CustomEventFieldUpdater implements Receiver<Bitmap> {
    @Override
    public void pass(Bitmap image) {
    //TODO
    }
}

-

public class CustomEntryFieldUpdater implements Receiver<Bitmap> {
    @Override
    public void pass(Bitmap image) {
    //TODO
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文