How do you efficiently implement an observer pattern if the object is a huge container?

We all know the observer pattern : you have a topic that can notify and update the list of observers about state changes. Now suppose that the object that you would like to observe is a container, and you would like to observe the container itself, that is, the addition and removal of elements, as well as the contained elements, that is, the state of updates to the elements of the container.

How would you implement the update mechanism so that it is fast with respect to inserting and deleting elements while storing a huge number of objects in your container? In particular,

  • Would you use the same type of container in a local copy of watchers?
  • Is there a reasonable choice of container that observers should use? (For example, it would be faster, say, to always use balanced trees, even if you are observing a linked list?)
  • How do you quickly translate an iterator into an observable container into an iterator into an observer container? (Trivial for arrays, hard for linked lists?)

If your container is a linked list, for example, you can insert items at a constant time. If m observers have to iterate over a list containing n elements, then the update takes O (n * m) the expected time.

If your container is an array, then changing the element takes constant time, and updating the m observers takes O (m) if you pass the index of the element O (n * m) if the observers have to go through the array.

If this helps, consider the following examples:

1. . , , - . - , . , , .

2. , -. , , - ( , , ...). , , . ( , , 53- , - Doe).

, "53rd St" "Dijkstra St"?

+3
2

- .

, . , , , , , (, , , ).

[EDIT] , " ". " ". , . - , , .

, (, ), , . , , " ".

, . - . , , .

" ". , , . , , , .

[EDIT2] , . . , . . " X " , . (, - -), , , .

, . Google , - . "" ( " " ). "sitemap", - , Google .

, , , . , , .

[EDIT3] :

  • . Qt , (, ) . , , . , . , (, Qt) . , . , , , ( , ).

  • Google sitemap - , , , URL- (HTTP HEAD HTTP GET).

  • Windows Linux , . , , , . , , . , , , , . , , , , , .

  • . , , - .

  • , ( ) ( , N M ). " " " X " " Y" ( " " X Y "- != X).

+4

?

. ().

. ( ).

+1

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


All Articles