SlideShare a Scribd company logo
1 of 21
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
Software Design Pattern Observer Explained
Software Design Pattern Observer Explained

More Related Content

Viewers also liked

Business Reading books of Leader@Work
Business Reading books of Leader@WorkBusiness Reading books of Leader@Work
Business Reading books of Leader@WorkOLCAY ARICAN
 
Hot Sports Program February2017
Hot Sports Program February2017Hot Sports Program February2017
Hot Sports Program February2017Nova Media
 
In-Store Merchandising - Store Design Guide
In-Store Merchandising - Store Design GuideIn-Store Merchandising - Store Design Guide
In-Store Merchandising - Store Design GuideDanielBooklin
 
İnterneti̇ Doğru kullanmak
İnterneti̇ Doğru kullanmakİnterneti̇ Doğru kullanmak
İnterneti̇ Doğru kullanmakozgrymn
 
Design for nondesigners 101 - 5 Elements to Include in Your Brand Guide
Design for nondesigners 101 - 5 Elements to Include in Your Brand GuideDesign for nondesigners 101 - 5 Elements to Include in Your Brand Guide
Design for nondesigners 101 - 5 Elements to Include in Your Brand GuideSusan Hope Bard
 
экономическое обозрение 1 полугодие 2015
экономическое обозрение 1 полугодие 2015экономическое обозрение 1 полугодие 2015
экономическое обозрение 1 полугодие 2015Anastasia Vinogradova
 
Halim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-General
Halim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-GeneralHalim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-General
Halim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-GeneralHalim Hani 03 Retail
 

Viewers also liked (10)

Business Reading books of Leader@Work
Business Reading books of Leader@WorkBusiness Reading books of Leader@Work
Business Reading books of Leader@Work
 
Hot Sports Program February2017
Hot Sports Program February2017Hot Sports Program February2017
Hot Sports Program February2017
 
In-Store Merchandising - Store Design Guide
In-Store Merchandising - Store Design GuideIn-Store Merchandising - Store Design Guide
In-Store Merchandising - Store Design Guide
 
İnterneti̇ Doğru kullanmak
İnterneti̇ Doğru kullanmakİnterneti̇ Doğru kullanmak
İnterneti̇ Doğru kullanmak
 
Case P4
Case P4Case P4
Case P4
 
Written Assignment 1
Written Assignment 1Written Assignment 1
Written Assignment 1
 
Casestudy #2
Casestudy #2Casestudy #2
Casestudy #2
 
Design for nondesigners 101 - 5 Elements to Include in Your Brand Guide
Design for nondesigners 101 - 5 Elements to Include in Your Brand GuideDesign for nondesigners 101 - 5 Elements to Include in Your Brand Guide
Design for nondesigners 101 - 5 Elements to Include in Your Brand Guide
 
экономическое обозрение 1 полугодие 2015
экономическое обозрение 1 полугодие 2015экономическое обозрение 1 полугодие 2015
экономическое обозрение 1 полугодие 2015
 
Halim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-General
Halim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-GeneralHalim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-General
Halim Hani - IPCP001-IDEAS & Proposed CONCEPT PROJECTS-General
 

Similar to Software Design Pattern Observer Explained

Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptxSHAHZAIBABBAS13
 
Lecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptxLecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptxAhmedAlAfandi5
 
Gof design pattern
Gof design patternGof design pattern
Gof design patternnaveen kumar
 
Gang of Four in Java
Gang of Four in Java Gang of Four in Java
Gang of Four in Java Mina Tafreshi
 
CASE tools and their effects on software quality
CASE tools and their effects on software qualityCASE tools and their effects on software quality
CASE tools and their effects on software qualityUtkarsh Agarwal
 
walkmod - JUG talk
walkmod - JUG talkwalkmod - JUG talk
walkmod - JUG talkwalkmod
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmodwalkmod
 
MWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCMWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCUlrich Krause
 
Customizing ERModernLook Applications
Customizing ERModernLook ApplicationsCustomizing ERModernLook Applications
Customizing ERModernLook ApplicationsWO Community
 
Beyond design patterns phpnw14
Beyond design patterns   phpnw14Beyond design patterns   phpnw14
Beyond design patterns phpnw14Anthony Ferrara
 
Implementing DDD with C#
Implementing DDD with C#Implementing DDD with C#
Implementing DDD with C#Pascal Laurin
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesUlrich Krause
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro anshu_atri
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBeroza Paul
 

Similar to Software Design Pattern Observer Explained (20)

Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptx
 
Lecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptxLecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptx
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Gang of Four in Java
Gang of Four in Java Gang of Four in Java
Gang of Four in Java
 
CASE tools and their effects on software quality
CASE tools and their effects on software qualityCASE tools and their effects on software quality
CASE tools and their effects on software quality
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
walkmod - JUG talk
walkmod - JUG talkwalkmod - JUG talk
walkmod - JUG talk
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
MWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCMWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVC
 
Customizing ERModernLook Applications
Customizing ERModernLook ApplicationsCustomizing ERModernLook Applications
Customizing ERModernLook Applications
 
Beyond design patterns phpnw14
Beyond design patterns   phpnw14Beyond design patterns   phpnw14
Beyond design patterns phpnw14
 
L05 Design Patterns
L05 Design PatternsL05 Design Patterns
L05 Design Patterns
 
Implementing DDD with C#
Implementing DDD with C#Implementing DDD with C#
Implementing DDD with C#
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPages
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
 
Design pattern
Design patternDesign pattern
Design pattern
 

Recently uploaded

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Recently uploaded (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

Software Design Pattern Observer Explained

  • 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
  • 10. Click to edit Master text styles Class Diagram
  • 11. Click to edit Master text styles Sequence Diagram
  • 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
  • 15. 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…
  • 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