akash89
akash89

Reputation: 891

Is communication between two fragments through an activity can be called as an Observer Pattern in android?

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

Answers (1)

razzledazzle
razzledazzle

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

Related Questions