RxJava:如何订阅不同类的事件

huangapple go评论65阅读模式
英文:

RxJava: How to subscribe to the events of a different class

问题

我对于如何概念上地创建一个观察者(Observer)并将其与另一个类相连接有一个问题:我目前有一个名为Simulation的类,它应该创建TransactionCreated对象并将其作为事件发布。另一个名为TransactionReceiver的类应该是Simulation类发布的每个事件的观察者,并与这些事件一起工作。
主方法包含在Simulation类中,并通过在静态上下文中创建事件并将其发布来启动,这是有效的。我的问题是,我应该如何将TransactionReceiver连接为观察者,并允许它通过在方法中接收事件并处理接收到的对象?我是否需要创建另一个类,该类将包含主方法,并创建连接为Observable和Observer的SimulationTransactionReceiver对象?那会是什么样子?
如果我通过几个不同的类扩展该系统,它们是否都必须通过一个连接观察者和可观察对象的类相连接?

英文:

I have a question about how to conceptually create an Observer and link it to another class: I currently have a class called Simulation that is supposed to create TransactionCreated objects and publish them as events. Another class called TransactionReceiver is supposed to be an Observer of every event that is published by the Simulation class and work with them.
The main method is included in the Simulation class and starts by creating an event in a static context and publishing it which works. My question would be how I am supposed to connect the TransactionReceiver as an Observer and let it subscribe to those events by receiving them in a method and work with those received objects? Do I need to create another class that would include the main method and create a Simulation and TransactionReceiver object that are then linked together as Observable and Observer? How would that look like?
And if I would extend that system with several different classes would they all have to be linked together through one class that connects Observers and Observables?

答案1

得分: 1

你的应用程序应该只有一个 main 方法。

从概念上讲,这个方法应该用于对 SimulationTransactionReceiver 进行初始设置,因此你可以将它移到一个单独的类中,以帮助你更好地可视化事物的工作方式。你可以尝试以下类似的写法:

class Application {
   private Simulation simulation;
   private TransactionReceiver transactionReceiver;

   public Application() {
       simulation = new Simulation(/* 这里填写参数*/);
       transactionReceiver = new TransactionReceiver(/*这里填写参数*/);
   }

   public void go() {
       simulation.simulate().subscribe(transactionCreated -> transactionReceiver.doSomething(transactionCreated));
   }

   public static void main(String[] args) {
       Application application = new Application();  
       application.go();
   } 
}

随着你变得更加熟练,你可以考虑添加依赖注入框架,比如 Guice 或 Dagger。

这将帮助你管理应用程序中需要使用的类的依赖关系。

因此,最终你将会得到一个更简单的 Application 类 - 它只需设置 DI 框架,然后你可以按照自己的方式使用这些类。

更新:

如果你想在两个不同的类之间进行通信,你将需要使用方法:

class Simulation {
    public Observable<TransactionCreated> simulate() {
        // 使用 PublishSubject 或其他方式
    }
}
英文:

Your app should only have one main method.

Conceptually, this should be where you do the initial setup of Simulation and TransactionReceiver, so perhaps you could move it to a separate class to help you visualise how things should work. You could try something like below:

class Application {
   private Simulation simulation;
   private TransactionReceiver transactionReceiver;

   public Application() {
       simulation = new Simulation(/* params here*/);
       transactionReceiver = new TransactionReceiver(/*params here*/);
   }

   public void go() {
       simulation.simulate().subscribe(transactionCreated -&gt; transactionReceiver.doSomething(transactionCreated);
   }

   public static final main(String[] args) {
       Application application = new Application();  
       application.go();
   } 
}

Eventually as you get more fluent you could think about adding a dependency-injection framework like Guice or Dagger.

This will help you with managing the dependencies of the classes that you need to use throughout your application.

So you would end up with a more simple Application - it would just set up the DI-framework and then you can use the classes how you want to.

UPDATE:

If you want to communicate between two different classes, you will need to use methods:

class Simulation {
    public Observable&lt;TransactionCreated&gt; simulate() {
        // use PublishSubject or whatever 
    }
}

huangapple
  • 本文由 发表于 2020年7月25日 20:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63088597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定