Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Observer design pattern


Published on

  • Be the first to comment

Observer design pattern

  1. 1. Research week #2
  2. 2.  Introduction Intent Class diagram Sequence diagram Benefit Implementations Issues Observer pattern in java
  3. 3.  Observer design pattern is behavioral pattern Used to assure consistency between objects. Separation of the objects that is dependent on each other. It used to make relation between objects at run time not compile time. Used in MVC(mainly Model & GUI part)
  4. 4.  Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Object that changes called “Subject”. Object that receives updates called “Object”.
  5. 5.  Sequence diagram observer Subject Register/att ach Change state trigger notify()/upda notification te getstate()
  6. 6.  Minimal coupling between the Subject and the Observer: Can reuse subjects without reusing their observers and vice versa. Observers can be added without modifying the subject. All subject knows is its list of observers so make decoupling. Subject does not need to know the concrete class of an observer.
  7. 7.  How does the subject keep track of its observers? Array, linked list, Vector What if an observer wants to observe more than one subject? subject tell the observer who it is via the update interface. Who triggers the update? The subject whenever its state changes. Can an observer also be a subject? Yes! Because class can implement more than one interface.
  8. 8.  How does Subject send the changed data to Object? Two ways: 1-Pull model: Observer invoke method requesting data SubjectName.getdata(); 2-Push model: Subject passes data to observer as argument at update () Object[i].update(SubjectName,data);
  9. 9.  We could implement the Observer pattern from scratch in Java. But Java provides the Observable/Observer classes as built-in support for the Observer pattern. The java.util.Observable class is the base Subject class. Any class that wants to be observed extends this class. Provides methods to add/delete observers Provides methods to notify all observers Uses a Vector for storing the observer references
  10. 10.  The java.util.Observer interface is the Observer interface. It must be implemented by any observer class. Java.util.Observable class public synchronized void addObserver(Observer o) public synchronized void deleteObserver(Observer o) protected synchronized void setChanged() public void notifyObservers() public void notifyObservers(Object arg)
  11. 11.  Java.util.Observer interface • public abstract void update(Observable o, Object arg) Let’s see sample code:
  12. 12. import java.util.Observable;import java.util.Observer;public class ConcreteSubject extends Observable { private String name; private float price; public ConcreteSubject(String name, float price) { = name; this.price = price; System.out.println("ConcreteSubject created: " + name + " at " + price); } public String getName() {return name;} public float getPrice() {return price;} public void setPrice(float price) { this.price = price; setChanged(); notifyObservers(new Float(price)); }}
  13. 13. import java .util.Observer;import java .util.Observable;public class ConcreteObserver implements Observer { private float price; public void NameObserver() { price =0; System.out.println("price observer is created is"+price);} public void update(Observable obj, Object a) { price = ((Float)a).floatValue(); System.out.println("PriceObserver: Price changed to " + price); }}
  14. 14. public class TestObserver { /** * @param args */ public static void main(String[] args) { ConcreteSubject s = new ConcreteSubject("GUI team",1.29f); ConcreteObserver o = new ConcreteObserver(); s.addObserver(o); s.setPrice(4.56f); s.setPrice(2.3f); }}
  15. 15.  Program outputConcreteSubject created: GUI team at 1.29PriceObserver: Price changed to 4.56PriceObserver: Price changed to 2.3
  16. 16.  /patterns/observer.html ml us/library/ee817669.aspx s/Observer.pdf