While researching Observer pattern
I have noticed, that the connection between the Subject
and the Observer
is usually one to many. Why so? Is there any particular reason why many-to-many relationship could cause any problems?
The system being designed using an Observer design pattern is done to provide the facility to a subject to have multiple observers and notify them whenever the subject's state changes. That is the textbook definition.
Lets take a real world use case - Newspaper and subscribers. A newspaper company sends out newspapers to people subscribed to it. It publishes newspapers and people subscribe to it.
As per your query, those subscribers can subscribe to many newspapers. So, it should be many-to-many. It appears so if you consider the system being designed and developed catering to all subscribers and all newspapers. Then this subscriber-newspaper universe is many-to-many. And you are right. As was discussed in the comment above - Twitter handles have multiple subscribers who subscribe to multiple twitter handles. Many-to-many it is.
But then there is a catch.
Actually to answer your question - we need to break down this many-to-many relation between publisher-subscriber into two parts- 1. One-To-Many from publisher to subscriber 2. One-To-Many from subscriber to publisher
One-To-Many from publisher to subscriber is used all the time - newspaper publisher distributes newspapers to its subscribers, tweets appear in followers feed etc.
But then do you really have any use for the one-to-many relationship back from the subscriber to publisher. I mean take the case of multiple folks and twitter handles they subscribe to. Suppose one subscriber tweets something. Does this information flow in the reverse direction to the twitter handles he/she follows? No. Do the twitter handles know that their followers have tweeted something?No. Information does not flow in the reverse direction from subscriber to publisher ,or, observer to subject. Then if there is no information flowing from observer to subject - why design for it! That is your answer.
Subject
's (Publisher
's) point of view. And in that regard, the Subject
does not really care whether the Observer
(Subscriber
) has any other Subject
s? –
Brachylogy Although there are usually multiple observers of the observable (sometimes called the subject), I don't think there have to be more than one. Agreed, there could be observers observing a data model and presenting the data as a pie chart, a graph and a table. I have a case where I have three Android Fragments changing my DataModel (the Observable) but only one Observer (my Main Activity) that summarizes the current state.
© 2022 - 2024 — McMap. All rights reserved.
Subject
) and post your news and many people (Observer
) can observe your twitter account to get notified about your news. so you have a one-to-many relationship. – PaginateObserver
) could follow multiple twitter accounts (Subject
). That would render the connection to be many-to-many. Am I wrong? – Brachylogy