How much should you do your observed / listened data?

You have a pull-oriented Observable / Listenable that notifies Observers / Listeners when some state changes.

A state consists of more than one nugget of data, and some of your observers / listeners do not care about the entire state.

As a rule, do you generally notify all observers / listeners and allow them to ignore notifications when nothing is taken care of?

Or do you usually prefer a separate observable for each “nugget” of data so that your observers / listeners guarantee the receipt of notifications that they need to respond to?

It depends on situation?

Do you have any general thoughts regarding the granularity of your observables / Listenables?

+3
source share
4 answers

You trade maintenance costs versus shipping costs. If you have small-scale definitions of events, each observer gets only what he needs, so you do not pay the delivery overhead to observers who are not interested, but this saves costs because each new type of nugget needs to be added to in some way .

In Pub / Sub messaging systems, where delivery costs are relatively high (messages spreading over networks), it is usually necessary to pay close attention to topics. It is often useful to use a carefully designed theme hierarchy. So we get patterns like

  sport
       football
              england 
                    premier
                    champioship
              scotland
                    spl
              france
                    ...
       cricket
              australia
                    ...
              india
              sri lanka

, . ( )

    sport/football/england/championship/watford
+2

, , , , , .

, , - . , , , , , . .

+1

, , , Observable, . .

0

. Observables Observers, .

The Gang of Four has a section for this very problem, and they advise not only push and pull models. The traction model may be ineffective, the push model cannot be reused.

Thus, it is highly dependent on the situation. I lean a little higher than the pulling model.

0
source

Source: https://habr.com/ru/post/1717416/


All Articles