观察者模式还是回调? [英] Observer pattern or Callback?

查看:65
本文介绍了观察者模式还是回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须设计一个 DownloadManager ,但是我的主要问题与 Download 可以发送到 DownloadManager 类似于 onUpdate()来更新进度条, onError() onFinish()等.code>必须从其 Download s接收此通知.

I have to do a design of a DownloadManager, but my main question is related to the notifications that a Download can send to the DownloadManager like onUpdate() to update a progress bar, onError(), onFinish(), etc. Somehow the DownloadManager has to receive this notifications from its Downloads.

我想过2种可能的方法:

I've thought 2 possible ways:

  • 观察者模式
  • 回调

观察者模式

基本上,有1个可观察者和N个观察者.在我的情况下,DownloadManager是观察者,而Downloads是观察者,因此关系是N Observables 1 Observer,正好相反.

Basically there are 1 Observable and N Observers. In my case the DownloadManager has te be an Observer and the Downloads the Observables, so the relation is N Observables 1 Observer, just the opposite.

优点是将所有可能的通知集中在一种方法中,在我的情况下,是观察者的 notify() update()(来自Java)方法只有DownloadManager.我可以使用通知代码将参数传递给notify()方法.

The advantage is to centralize all the possible notifications in one method, the notify() or update() (from java) method from the Observers, in my case only the DownloadManager. I can pass a param to the notify() method with the code of the notification.

劣势?我正在使用oop模式来完成可以通过回调轻松完成的操作.另外,N个可观察者1个观察者这很奇怪,至少对于观察者模式而言,因为此模式是针对1个可观察的N个观察者完成的,所以我真的不会使用观察者模式.

Disadvantage? I'm using an oop pattern for a thing that can be done easily with a callback. Also, N observables 1 observer it's something weird, at least with the observer pattern because this pattern was done for 1 observable N observers, so I really won't be using the observer pattern.

回调

非常类似于观察者模式.DownloadManager实现侦听器"(界面).此侦听器实现通知功能onFinish(),onUpdate()等.然后,必须在所有下载中注册此侦听器,因此,当下载完成时,它将调用 listener.onFinish().另外,我可以像从观察者模式一样,从下载中将参数传递给此方法.

Very similar to the observer pattern. The DownloadManager implements a "listener" (interface). This listener implements the notification functions onFinish(), onUpdate(), etc. Then this listener must be registered in all Downloads, so when a Download finishes it will call listener.onFinish(). Additionally I can pass parameters to this methods from the Downloads, like in the observer pattern.

优点:易于使用.缺点:无.

Advantage: Easily usage. Disadvantage: None.

我可能会使用回调,因为我认为对1个观察者N个可观察变量使用观察者模式是没有意义的.

I will probably use a callback because in my opinion it makes no sense to use an observer pattern for 1 observer N observables.

那么,您将使用哪个选项?

And you, which option will use?

推荐答案

还有一个选项,与观察者/回调"方法相反.您可以将 DownloadManager 的客户端从演出的被动播放器切换为主动播放器.他们会定期请求其状态,而不是等待经理的消息.

There is also one option, which is opposite to Observer/Callback approach. You can turn clients of DownloadManager from passive to active players of the show. Instead of waiting for a message from the manager they will regularly request its status.

这样,对于最终用户而言,您的下载过程将变得更加流畅.而且您将能够更好地控制它.

This way your download process will look much smoother to the end-user. And you will be able to control it better.

当然,您将必须使用两个线程.否则,您将必须教 DownloadManager 一步一步地工作,将控制权交还给客户端,而不是运行一项坚不可摧的工作.

Of course, you will have to use two threads. Or you will have to teach the DownloadManager to work in small steps, returning control to its client, instead of running one unbreakable piece of work.

这篇关于观察者模式还是回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆