Observer Pattern — get the gist in 2 min.

aditya chaudhari
JavaDeveloperDiary — JDD
2 min readJun 28, 2022
observer patter structure

The basic idea behind observer pattern is to have a publisher — subscriber like mechanism , where publisher wants to notify multiple subscriber objects regarding some events / state change which is the in interest of subscriber.

An observer pattern is a type of behavior design pattern.

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Simple Example,
inStock notification
and newsletter subscriptions are the classic example of an observer pattern implementation from the eCommerce domain.

The diagram above explains the high-level component flow diagram for inStock notification example .

For complete java code and example please refer this detailed post

Structure and different components in observer pattern.

Publisher (ObservableProduct) : Publisher publishes the event which is of the interest to the observers which are subscribed to the publisher.

Subscriber (ObserverCustomer): subscriber interface describes the update mechanism for the event/state which is published by the Publisher.

and respective ConcretePublisher (Product) and ConcreteSubscriber (Customer) helps implement the behaviors .

Observer Pattern helps implement loosely couple design between objects it interacts.

The real fun is in the details, if you wish to know and understand the pros-cons, java-code example for observer pattern, please consider checking detailed article at [observer-pattern-java-explained-5-min-read ].

Thanks for reading. Love IT, Live IT, Enjoy IT.

On the journey to understand the core essence of design patterns.
Every Tuesday I publish a small 2 min post and a long 6–10 min post along with a code example, please consider following if you wish to get notified!

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT