Why is Observer design pattern usually portrayed as one-to-many?
Asked Answered
B

2

5

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?

Brachylogy answered 6/1, 2016 at 9:50 Comment(2)
See it like twitter? You have a twitter account (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.Paginate
But in this scenario other people (Observer) could follow multiple twitter accounts (Subject). That would render the connection to be many-to-many. Am I wrong?Brachylogy
B
7

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.

Barri answered 6/1, 2016 at 12:16 Comment(4)
So I guess what you are saying is that the Observer pattern is presented from the Subject's (Publisher's) point of view. And in that regard, the Subject does not really care whether the Observer (Subscriber) has any other Subjects?Brachylogy
Yes. It may be a concern of the overall system governing these publisher-subscriber universe. For eg. twitter as a whole might have some policies which may worry about who-all the subscriber follows. But from a basic subject-observer context in observer design pattern implementation the Subject does not care.Barri
That sits well with me. Thank you for the answer.Brachylogy
For further discussion on this I'd recommend reading Eric Evans's book Domain Driven Design. He makes a very similar point, that by isolating the direction that the relationship works in there are 2 effects. On the one hand the model is simplified (1:n relationships are easier to reason about), while on the other an important aspect of the model is highlighted. Of course, in different domains you might find the direction differing, but in any given domain one direction is more helpful than the other.Callan
T
0

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.

Tollmann answered 15/1, 2016 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.