This document discusses the observer design pattern. It defines the observer pattern as establishing a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. It provides examples of when to use the observer pattern, such as with a stock market notifying individual stocks of updates. The document outlines the structure of the observer pattern including class and sequence diagrams and provides a code demo and pros and cons.
1. Click to edit Master text styles
Presentation
On
Software Design Pattern
2. Software Design Pattern
Observer Pattern
R. Nirthika 2012/SP/142
S. Dharshika 2012/SP/040
T. Janani 2012/SP/035
S.Kugarajeevan
2012/CSC/014
3. Click to edit Master text styles
What is Design Pattern?
4. Click to edit Master text styles
What is Design Patterns?
• In software engineering, a design pattern is a general reusable solution to a commonly
occurring problem within a given context in software design - Wikipedia
• It is a description for how to solve a problem that can be used in many different situations
5. Click to edit Master text styles
Software
Design Pattern
Creational
Structural
Behavioral
Types Of Design Patterns
• Used to construct objects such that they can
be decoupled from their implementing
system.
• Used to form large object structures
between many disparate objects.
• Used to manage algorithms, relationships,
and responsibilities between objects
6. Click to edit Master text styles
Behavioral
• Chain of Responsibility
• Command
• Interpreter
• Iterator
• Mediator
• Memento
• Observer
• State
• Strategy
• Template
• Visitor
Design Patterns
Behavioral
• Chain of Responsibility
• Command
• Interpreter
• Iterator
• Mediator
• Memento
• Observer
• State
• Strategy
• Template
• Visitor
7. Click to edit Master text styles
Observer Design Pattern
Define a one-to-many dependency between objects so that when one object changes
state, all its dependents are notified and updated automatically.
8. Click to edit Master text styles
When to use the observer pattern
When you need many other objects to receive an update when another object changes
Example
Stock market with thousands of stocks needs to send updates to objects representing individual stocks.
The subject [Publisher] sends many stocks to the observers.
The observers [subscribers] takes the ones they want and use them.
1
9. Click to edit Master text styles
Structure Of Observer
12. Click to edit Master text styles
Observer Design Pattern Real Time Example
13. Click to edit Master text styles
Demo
public interface Subject {
public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObservers();
}
Subject.java
14. Click to edit Master text styles
Demo
import java.util.ArrayList;
public class Product implements Subject{
private ArrayList<Observer> observers = new ArrayList<Observer>();
public void registerObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
Product.java
16. Click to edit Master text styles
Demo
public void setAvailability(String availability) {
this.availability = availability;
notifyObservers();
}
public void notifyObservers() {
System.out.println("Notifying to all the subscribers when "+productName+" "+productType+"
became available");
for (Observer ob : observers) {
ob.update(productName,productType,availability );
}
System.out.println();
}
}
Continue…
17. Click to edit Master text styles
Demo
public interface Observer {
public void update(String productName, String productType,String availability);
}
Observer.java
public class Person implements Observer{
String personName;
public Person(String personName) {
this.personName = personName;
}
public void update(String productName, String productType,String availability) {
System.out.println("Hello "+personName+", "+productName+" "+productType+" is now "+availability+" on flipkart");
}
}
Person.java
18. Click to edit Master text styles
Demo
public class ObserverPatternMain {
public static void main(String[] args) {
Person arpitPerson=new Person("Arpit");
Person johnPerson=new Person("John");
Product Camera=new Product("Canon", "Camera", "Not available");
Camera.registerObserver(johnPerson);
Camera.setAvailability("Available");
Product samsungMobile=new Product("Samsung", "Mobile", "Not available");
samsungMobile.registerObserver(arpitPerson);
samsungMobile.setAvailability("Available");
}
ObserverPatternMain.java
19. Click to edit Master text styles
Observer Pattern Pros & Cons
Pros:
• Supports the principle to strive for loosely coupled designs between objects that interact.
• Allows you to send data to many other objects in a very efficient manner.
• No modification is need to be done to the subject to add new observers.
• You can add and remove observers at anytime.
Cons:
• If not used carefully the observer pattern can add unnecessary complexity
• The order of Observer notifications is undependable