Reputation: 891
I just wanted to clarify one thing related to Observer Pattern in java.
Observer pattern is used when there is a one-to-many relationship between objects, such as if one object is modified, its dependent objects are to be notified automatically. Observer pattern falls under behavioural pattern category.
This is the definition of an Observer Pattern that I got.
http://www.tutorialspoint.com/design_pattern/observer_pattern.htm
The link also suggests the implementation.
My query related to observer pattern is, We have the concept of event bubbling technique (or the listener technique) that we use when we want to communicate data from Fragment A to Fragment B, both hosted by same Activity C.
Simple question is - can we call this technique similar to Observer Pattern ?
If interviewer asks me, cite an example of Observer Pattern, can I quote this example. My assumption is YES, this technique is OBSERVER PATTERN, however, I do need some confirmed opinion.
Upvotes: 1
Views: 625
Reputation: 6930
The definition you posted mentions one-to-many relationship
between the caller and the callee, which is very true.
But considering Activity and Fragments, they're normally one-to-one relation. This is still valid when your Activity acts as a middle-man between one or more fragments.
Rather than Observer pattern, the pattern used in Activity and Fragment communication is the Callback pattern, where the target instance implements an interface with which the caller can invoke.
Upvotes: 1