Click to edit Master text styles
Presentation
On
Software Design Pattern
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
Click to edit Master text styles
What is Design Pattern?
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
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
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
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.
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
Click to edit Master text styles
Structure Of Observer
Click to edit Master text styles
Class Diagram
Click to edit Master text styles
Sequence Diagram
Click to edit Master text styles
Observer Design Pattern Real Time Example
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
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
Click to edit Master text styles
Demo
private String productName;
private String productType;
String availability;
public Product(String productName, String productType,String availability) {
super();
this.productName = productName;
this.productType = productType;
this.availability=availability;
}
Continue…
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…
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
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
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
Observer Software Design Pattern
Observer Software Design Pattern

Observer Software Design Pattern

  • 1.
    Click to editMaster text styles Presentation On Software Design Pattern
  • 2.
    Software Design Pattern ObserverPattern R. Nirthika 2012/SP/142 S. Dharshika 2012/SP/040 T. Janani 2012/SP/035 S.Kugarajeevan 2012/CSC/014
  • 3.
    Click to editMaster text styles What is Design Pattern?
  • 4.
    Click to editMaster 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 editMaster 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 editMaster 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 editMaster 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 editMaster 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 editMaster text styles Structure Of Observer
  • 10.
    Click to editMaster text styles Class Diagram
  • 11.
    Click to editMaster text styles Sequence Diagram
  • 12.
    Click to editMaster text styles Observer Design Pattern Real Time Example
  • 13.
    Click to editMaster text styles Demo public interface Subject { public void registerObserver(Observer observer); public void removeObserver(Observer observer); public void notifyObservers(); } Subject.java
  • 14.
    Click to editMaster 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
  • 15.
    Click to editMaster text styles Demo private String productName; private String productType; String availability; public Product(String productName, String productType,String availability) { super(); this.productName = productName; this.productType = productType; this.availability=availability; } Continue…
  • 16.
    Click to editMaster 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 editMaster 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 editMaster 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 editMaster 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