观察者和订阅者之间有什么区别? [英] What is the difference between an Observer and a Subscriber?

查看:237
本文介绍了观察者和订阅者之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试破译以下功能:

I am trying to decipher the following function:

Subscription getCar(id, Observer<Car> observer) {
    return getCarDetails(id, new Observer<CarDetails> {
                             @Override
                             onNext(CarDetails details) {           
                                 observer.onNext(details.getCar());
                             } });
}

我从http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/但它只提到了Observer,说你将大部分时间都使用Subscriber消费者从Observable发出的物品。

I got a good intro to rxjava from http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/ but it only mentioned Observer in passing, saying you'll be using Subscriber most of the time to consumer items emitted from an Observable.

有人可以向我解释


  1. 什么是观察员?

  2. 观察者与订阅者的不同之处是什么?

  3. 上面的代码片段有什么作用?

Javadoc 让它看起来像订阅者。订阅者的javadoc说它实现了观察者和订阅。我很困惑。

Javadoc made it seem just like a subscriber. The javadoc for subscriber says it implements observer and subscription. I am very confused.

推荐答案

已编辑:@ Alrid的评论

EDITED: with @Alrid's comment

tl; dr

public abstract class Subscriber<T> implements Observer<T>, Subscription

所以订阅者 Observer ,在订阅时有额外的语义(更多的是关于取消订阅)。
你问题中的代码只显示它传递了 Observer 接口,而不是实现(通常的编程实践)。

So a Subscriber is an implementation of the Observer, with additional semantics on subscription (it's more about un-subscription). The code in your question just shows that it passes the Observer interface, instead of the implementation (usual programming practice).

此代码还返回订阅,这可能是因为此代码的作者认为客户端应该只可以访问订阅方法,无需访问observable生成的元素。这可能是程序员错误。

Also this code returns a Subscription, that may be because the author of this code thought that the client should only have access to Subscription methods, without access to elements produced by the observable. That may be a programmer error.

长篇故事

你真的应该读本网站(或书籍)的内容: http://www.introtorx.com
这是关于Rx.Net,但概念是一样的,它们是由Erik Meijer和RxJava实现者创建的,遵循他们(如果适用于Java语言)。

Really you should read the content of this website (or book) : http://www.introtorx.com It is about Rx.Net, but the concepts are the very same, they were created by Erik Meijer and RxJava implementors followed them (if applicable to the Java language).

此页面将引起您的兴趣(这是第二章): KeyTypes

This page will interest you (it is the second chapter) : KeyTypes

在这里你'请阅读第一段:

Here you'll read in the first paragraphs :


使用Rx时需要了解两种关键类型,以及有助于帮助的辅助类型的子集你可以更有效地学习Rx。 IObserver和IObservable构成了Rx的基本构建块,而ISubject的实现减少了新开发者的学习曲线。

There are two key types to understand when working with Rx, and a subset of auxiliary types that will help you to learn Rx more effectively. The IObserver and IObservable form the fundamental building blocks for Rx, while implementations of ISubject reduce the learning curve for developers new to Rx.

。 ..


本质上,Rx建立在Observer模式的基础之上。 .NET已经公开了实现Observer模式的一些其他方法,例如多播委托或事件(通常是多播委托)。

Essentially Rx is built upon the foundations of the Observer pattern. .NET already exposes some other ways to implement the Observer pattern such as multicast delegates or events (which are usually multicast delegates).

Even如果类型/ API有点不同,你会对这本书有很多了解,可能比一些博客更多。

Even if types / API are a bit different, you will learn a lot with this book, probably way more than with some blogs.

这本书是什么不说 ...因为它在RxJava实现中

RxJava主要开发人员时间引入了一个微小的变化(参见PR #792 ),可以区分两种类型的合同:

RxJava main developer at this time introduced a slight variation (see PR #792) that allowed to distinguish two types of contracts :


  • 通知 - > 观察员

  • (联合国)订阅 - > 订阅

  • notification -> Observer
  • (un)subscription -> Subscription

此更改可以更好地表达/拆分RxJava库的实现类的这些问题。

This change allowed to better express/split these concerns of the implementing classes of the RxJava library.

但是作为库用户,使用实际的实现RxJava库的s应该足够好。

However as a library user, using actual implementations of the RxJava library should be good enough.

实现订阅者需要更多的知识,工作和关注,实际上订阅语义非常重要,具体取决于源可观察的类型(热或冷?创建昂贵) ?)

Implementing a subscriber require much more knowledge, work and care, indeed the subscription semantics are very important depending on the type of the source observable (Hot or cold? Expensive to create ?)

公开订阅者而不是在大多数情况下不会干扰代码,但除非需要那些非订阅语义,否则它不是它的预期用途。但最终实现 Subscriber ,并可能涉及到一些陷阱,例如:

Exposing Subscriber rather than Observer in cases such as above will not interfere with the code in in most cases, but it is not the intended use for it unless those un-subscription semantics are needed. But in the end implementing a Subscriber, and may involve to fall in some pitfalls such as :


  1. 花费资源用于不使用的功能

  2. 无法从其他类继承

  3. 编写错误的非订阅代码

  4. 复制/粘贴代码不正确的代码或为不同的上下文编写的正确代码

  1. spend resources for functionality you will not use
  2. cannot inherit from another class
  3. write incorrect un-subscription code
  4. copy/paste code an incorrect code or correct code written for a different context

这篇关于观察者和订阅者之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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