开发者

A PictureListener that has a WebView data member - does this make sense?

开发者 https://www.devze.com 2023-03-01 23:25 出处:网络
As we all know, PictureListener is an interface with a single public method: onNewPicture(WebView view, Picture picture).

As we all know, PictureListener is an interface with a single public method: onNewPicture(WebView view, Picture picture).

It works for simple scenarios but in my case I need to handle various errors (due to the pred开发者_高级运维ictably unpredictable nature of the Internet, especially via WiFi/3G).

For that, I intend to create a slightly more complex class, implementing PictureListener and having some extra state information and associated handler functions. So far so good, but...

One of the function needs access to the WebView - the same WebView that is passed as a parameter in onNewPicture() but isn't available to PictureListener anywhere else.

So, I was thinking of passing that WebView as a parameter to the constructor of MyPictureListener and saving it as a private data member.

I haven't seen an example of such implementation before and I am not sure whether I would be violating a rule with which I am not familiar.

Does a PictureListener that has a WebView data member make sense? Are there pitfalls that I should be aware of?


No, it doesn't.

You won't be violating rules but you will be going against the "mindset" of the WebView & PictureListener design:

  1. PictureListener is an interface and doesn't receive WebView as a parameter.
  2. WebView, on the other hand, has a dedicated method for associating a PictureListener with it. So WebView "knows" about PictureListener.

Instead of passing a WebView parameter to PictureListener's constructor, just cast the view parameter in onNewPicture() to your WebView and call a method in your WebView that access anything in it.

0

精彩评论

暂无评论...
验证码 换一张
取 消