Observer Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Continuing with our design pattern articles, we will further dive into another popular and useful observer pattern implementation. In this article I will try to explain what is observer pattern and provide observer pattern implementation with a simple example.

Observer Pattern Implementation

The Gang of Four pattern defines observer design pattern as below

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

Let us study a sample diagram for observer pattern implementation.

Generic Observer class diagram

Note the components

  • Observable: Interface or abstract class defining operations for attaching and detaching observers to the client. Observable is also known as Subject
  • ConcreteObservable: ConcreteObservable notifies the observers when change in state of Observable occurs.
  • Observer: Interface or abstract class defining operations to be used to notify this object.
  • ConcreteObserver: Concrete Observer implementations which can have some additional methods for their own functionality.

 Example:

We have few assumptions for our example. Consider we have an online shopping website where users have the functionality that whenever price of an item changes, users should be notified. We will create below types.

  • Observable: This is the interface which is going to act as Observable and has three methods, register(), remove() and notifyObservers(), which do the task of registering a new observer, removing the observer and notifying all observers respectively.
  • Observer: This is the interface which is going to act Observer having update() method which is used for updating the observer that Observable has changed.
  • Product: Product class implements Observable and implements own setPrice() method which is used to set price dynamically. setPrice() will call notifyObservers() internally.
  • RegularBuyer: RegularBuyer implements Observer and overrides update() method which accepts newly set price which is called from observer. It can unRegister() itself from observable.
  • PrivilegedBuyer: PrivilegedBuyer implements Observer and overrides update()

Observer pattern Implementation Diagram:

observer example class diagram

Let us try to code our example for observer pattern implementation.

Observable.java

Observer.java

Product.java

RegularBuyer.java

PrivilegedBuyer.java

We are all done with observer pattern implementation part. Let us try to write a program to see it running.

OnlineShop.java

Output of Observer pattern implementation is as beow

Observer Pattern Implementation Flow:

  • We created a Product which is Observable and is able to register new observers, remove an observer and notify all observers. As soon as Product is created, a list of observers is created.
  •  Now you create a RegularBuyer, by passing Product to it, from RegularBuyer constructor, register() method of Product is called which means RegularBuyer has registered to Product as Observer.
  • Simillary, PrivilegedBuyer is registered as Observer for Product. At this points, all observers are listening to updates from Observable i.e. Product.
  • So when you call product.setPrice(10000); then internally it calls notifyObservers() from setPrice(); which in turn invokes update() method of all observers those are present in subscription list which is populated when each Observer is created. This is why we get first two lines of output. i.e. both RegularBuyer and PrivilegedBuyer observers are notified.
  • Now, when you unregister RegularBuyer using regularBuyer.unregister(); then it removes itself from subscription list.
  • Next time when you update price using product.setPrice(15000); then only PrivilegedBuyer is notified which can be seen from output of program.

I hope you understood my observer pattern implementation with simple example. We will continue further with another interesting design pattern implementation.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *