BlackBerry:使我的图像线程类可用于所有实例
我创建了一个图像线程类,它在加载图像的实例中运行更新方法:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义一个单独的接口类型,然后所有类都可以根据需要实现该接口类型,例如:
ImageThreadCallback.java:
ImageThread.java:
CustomEventField.java:
CustomEntryField.java:
Define a separate interface type that all of your classes can then implement as needed, eg:
ImageThreadCallback.java:
ImageThread.java:
CustomEventField.java:
CustomEntryField.java:
首先,ImageThread 可能应该扩展 Thread 或重命名为类似 ImageUpdateTask 的名称。
正如您在问题中提到的,它难以重用的原因是它耦合性太强。这可以通过将方法抽象为单独的接口来解决。
-
-
-
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.
-
-
-