SlideShare a Scribd company logo
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
Observer

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 Observer

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 Observer (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

Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismDeeptiGupta154
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfjoachimlavalley1
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
 

Recently uploaded (20)

Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 

Observer

  • 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