Ansh_s - PowerPoint PPT Presentation

About This Presentation
Title:

Ansh_s

Description:

www.acem.edu.in – PowerPoint PPT presentation

Number of Views:2
Updated: 30 October 2020
Slides: 13
Provided by: Ansh_s
Category:
Tags:

less

Transcript and Presenter's Notes

Title: Ansh_s


1
Observer Design Pattern in Java
2
  • "Define a one-to-many dependency between objects
    so that when one object changes state, all its
    dependents are notified and updated
    automatically".
  • You can think of observer design pattern in two
    ways
  • Subject-Observer relationshipObject which is
    being observed is refereed as Subject and classes
    which observe subject are called Observer 
  • Publisher-Subscriber relationshipA publisher is
    one who publish data and notifies it to the list
    of subscribers who have subscribed for the same
    to that publisher. A simple example is Newspaper.
    Whenever a new edition is published by the
    publisher,it will be circulated among subscribers
    whom have subscribed to publisher.
  • The observers will not monitor every time whether
    there is any change in state of subject or not,
    since they will be notified for every state
    change of subject, until they stop observing
    subject.

3
  • Some real life examples
  • You might have surfed "Flipkart.com-Online
    megastore".So when you search for any product and
    it is unavailable then there is option called
    "Notify me when product is available".If you
    subscribe to that option then when state of
    product changes i.e. it is available,you will get
    notification mail "Product is available now you
    can buy it".In this case,Product is subject and
    You are an observer.
  • Lets say,your permanent address is changed then
    you need to notify passport authority and pan
    card authority.So here passport authority and pan
    card authority are observers and You are a
    subject.
  • On facebook also,If you subscribe someone then
    whenever new updates happen then you will be
    notified.

4
When to use it
  • When one object changes its state,then all other
    dependents object must automatically change their
    state to maintain consistency
  • When subject doesn't know about number of
    observers it has.
  • When an object should be able to notify other
    objects without knowing who objects are.

5
 UML diagram for observer design pattern
6
Components
  • Subject
  • Knows its observers
  • Has any number of observer
  • Provides an interface to attach and detaching
    observer object at run time
  • Observer
  • Provides an update interface to receive signal
    from subject

7
  • ConcreteSubject
  • Stores state of interest to ConcreteObserver
    objects.
  • Send notification to it's observer 
  • ConcreteObserver
  • Maintains reference to a ConcreteSubject object
  • Maintains observer state consistent with
    subjects.
  • Implements update operation

8
Java in-built API for observer pattern 
  • The java API provides one class and one inteface
    for implementing observer pattern.
  • 1. java.util.Observable-class
  • 2. java.util.Observer-interfac

9
  • java.util.Observable 
  • For being observed,class must extend this class.
    The subclass becomes observable and override
    methods of  java.util.Observable and other
    objects can "observe" changes in state of this
    object.
  • Methods
  • addObserver(Observer o) add Observer to the list
    of observers for this subject.
  • deleteObserver(Observer o) delete Observers from
    the list of observers .
  • notifyObservers()  notify all the observers if
    object has changed.
  • hasChanged() return true if object has changed.
  • setChanged() This method marks object has
    changed
  • clearChanged() this method will indicate that
    subject has no changes or all the observers has
    been notified.

10
  • java.util.Observer 
  • The class that performs the "observing" must
    implement the java.util.Observer interface. There
    is a single method 
  • public void update(Observable obj, Object
    arg) This method is called whenever the observed
    object is changed. An application calls an
    Observable object's notifyObservers method for
    notifying to all the observers of change.

11
  • Example 
  • You might have surfed "Flipkart.com-Online
    megastore".So when you search for any product and
    it is unavailable then there is option called
    "Notify me when product is available".If you
    subscribe to that option then when state of
    product changes i.e. it is available,you will get
    notification mail "Product is available now you
    can buy it".

12
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com