Exchanging messages between different classes
Splitting the application into different domains independent of each other provides many great advantages, but it also creates a few challenges. For example, there are scenarios where you need to exchange data between two ViewModels; or you need the ViewModel to communicate with the View to trigger an operation on the UI layer, such as starting an animation.
Creating a dependency between multiple elements would break the MVVM pattern, so we must find another solution. One of the most adopted solutions to support these scenarios is the Publisher-Subscriber pattern, which enables every class in the application to exchange messages. What makes this pattern a great fit for applications built with the MVVM patterns is that these messages aren't tightly coupled: a publisher can send a message without knowing who the subscribers are, and subscribers can receive messages without knowing who the publisher is.
Let's see a concrete...