SlideShare uma empresa Scribd logo
1 de 75
Design Patterns 05/28/10 Week 5: Observer and Decorator Jonathan Simon [email_address]
Weather Monitoring Application ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Observer Pattern ,[object Object],[object Object],05/28/10
Definitions ,[object Object],[object Object],[object Object],[object Object],05/28/10
Interfaces 05/28/10 Contains signatures for registering, removing, and notifying observers. Contains Update method so that information can be sent from Subject to Observer.
WeatherData (Subject) Questions:   Which field is used for storing the list of Observers?  How does an Observer add itself to this list? The fields for humidity, pressure, and temperature represent the state of the Subject.
WeatherData (Subject; Pt 1) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Observers are notified since state has changed.
WeatherData (Subject; Pt2) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Here is how the Subject sends information to all Observers
Current Conditions Display (Observer) Note that the individual Observer is composed of the Subject. So the Subjects has  a reference to all Observers…and each Observer has a reference to a Subject. Questions:  What method on this Observer is called by the Subject when data is changed?
CurrentConditionsDisplay (Observer) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
WeatherStation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Lab - Push ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lab - Pull ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lab – Refactor WeatherData ,[object Object],[object Object],[object Object],05/28/10
Refactoring WeatherData  ,[object Object],[object Object],[object Object],05/28/10
Subject ,[object Object],[object Object],[object Object],[object Object],05/28/10
Subject ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10 What is wrong with NotifyObserver??
Subject ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10 Is this implying Push or Pull?
Observer ,[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Observer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
So far… 05/28/10 NotifyObserver will loop through each Observer and call Update() and pass a reference of itself Observer registers itself with the Subject
Concrete Subject ,[object Object],[object Object],[object Object],[object Object],05/28/10
ConcreteSubject ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Concrete Observer ,[object Object],[object Object],05/28/10
ConcreteObserver ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Client Code ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
The Big Picture 05/28/10
In Class Lab ,[object Object],[object Object],[object Object],[object Object],05/28/10
Multiple Events ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Problem ,[object Object],[object Object],05/28/10
Analysis ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Analysis ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Subject (Current Implementation) 05/28/10 Our Subject class contains the one notification event. Our ConcreteSubject gets the notification event by inheritance. We want our ConcreteSubject to contain multiple events.  Does Inheritance make sense??? Would could we use instead?
Subject/SubjectEvent 05/28/10 This class represents a specific event of the Subject.
SubjectEvent 05/28/10 Now the SubjectEvent, not Subject, contains a reference to all of the Observers who are interested in being notified.
Subject/SubjectEvent 05/28/10 The constructor of WeatherData takes a SubjectEvent as parameter.
WeatherData ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
SubjectEvent + Observers ,[object Object],[object Object],[object Object],[object Object],05/28/10
SubjectEvent  05/28/10 Added name parameter to the constructor and storing it in eventName Field.
SubjectEvent ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10 Cheating
Observer ,[object Object],[object Object],05/28/10
Observer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
CurrentConditionsDisplay ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Big Picture 05/28/10
Client Code ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Not done yet.. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
WeatherData (cont) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
ConcreteObserver ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Client Code ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Other Problems Arise.. ,[object Object],[object Object],05/28/10
Change Manager ,[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Features of the Change Manager ,[object Object],[object Object],[object Object],[object Object],05/28/10
Addressing Problem #2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Addressing Problem #2 (cont) ,[object Object],[object Object],[object Object],05/28/10
Using Event Infrastructure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Real World Examples ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Solution #1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Problem #2 ,[object Object],[object Object],[object Object],[object Object],05/28/10
Solution #2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Problem #3 ,[object Object],[object Object],[object Object],05/28/10
Problem #3 (Cont) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Solution #3 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Solution #3 ,[object Object],[object Object],[object Object],05/28/10
Lab (Introduction to Decorator) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Lab ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Lab ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lab ,[object Object],[object Object],05/28/10
Problem ,[object Object],[object Object],[object Object],05/28/10
Solution ,[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10 This is our Decorator class. In this example, it is extending the class that we want to enhance with additional functionality.
EncryptedLogger ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10 This is the object we want to extend
CompressionLogger ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Client Code ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10
Decorator ,[object Object],05/28/10
Things to Note ,[object Object],[object Object],[object Object],05/28/10
Comparison to Other Patterns ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],05/28/10

Mais conteúdo relacionado

Mais procurados

.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~
.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~
.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~
Michio Koyama
 

Mais procurados (20)

RxJS - The Basics & The Future
RxJS - The Basics & The FutureRxJS - The Basics & The Future
RxJS - The Basics & The Future
 
Law of Demeter & Objective Sense of Style
Law of Demeter & Objective Sense of StyleLaw of Demeter & Objective Sense of Style
Law of Demeter & Objective Sense of Style
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
Visitor Pattern
Visitor PatternVisitor Pattern
Visitor Pattern
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
Visitor pattern
Visitor patternVisitor pattern
Visitor pattern
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Angular 2 observables
Angular 2 observablesAngular 2 observables
Angular 2 observables
 
Java Concurrency - Quiz Questions
Java Concurrency - Quiz QuestionsJava Concurrency - Quiz Questions
Java Concurrency - Quiz Questions
 
.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~
.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~
.NETからActive Directoryデータにアクセス ~グループ情報の取得と表示~
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
 
State pattern
State patternState pattern
State pattern
 
EMC Documentum - xCP.x Updating Endpoint
EMC Documentum - xCP.x Updating EndpointEMC Documentum - xCP.x Updating Endpoint
EMC Documentum - xCP.x Updating Endpoint
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
State
StateState
State
 
How Sentry can help us with bugs
How Sentry can help us with bugsHow Sentry can help us with bugs
How Sentry can help us with bugs
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]
 
Angular 6 Form Validation with Material
Angular 6 Form Validation with MaterialAngular 6 Form Validation with Material
Angular 6 Form Validation with Material
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 

Destaque

Observer Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 AugObserver Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 Aug
melbournepatterns
 
Design patterns 4 - observer pattern
Design patterns   4 - observer patternDesign patterns   4 - observer pattern
Design patterns 4 - observer pattern
pixelblend
 

Destaque (20)

Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template Pattern
 
MVC and Other Design Patterns
MVC and Other Design PatternsMVC and Other Design Patterns
MVC and Other Design Patterns
 
Design patterns - Observer Pattern
Design patterns - Observer PatternDesign patterns - Observer Pattern
Design patterns - Observer Pattern
 
Decorator Design Pattern
Decorator Design PatternDecorator Design Pattern
Decorator Design Pattern
 
Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)
 
The Decorator Pattern
The Decorator PatternThe Decorator Pattern
The Decorator Pattern
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
 
Factory and Abstract Factory
Factory and Abstract FactoryFactory and Abstract Factory
Factory and Abstract Factory
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
 
Command and Adapter Pattern
Command and Adapter PatternCommand and Adapter Pattern
Command and Adapter Pattern
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Observer Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 AugObserver Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 Aug
 
Design patterns 4 - observer pattern
Design patterns   4 - observer patternDesign patterns   4 - observer pattern
Design patterns 4 - observer pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Design patterns: observer pattern
Design patterns: observer patternDesign patterns: observer pattern
Design patterns: observer pattern
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Design pattern - Software Engineering
Design pattern - Software EngineeringDesign pattern - Software Engineering
Design pattern - Software Engineering
 
Implementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgetsImplementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgets
 
15 decorator pattern
15 decorator pattern15 decorator pattern
15 decorator pattern
 
Phani Kumar - Decorator Pattern
Phani Kumar - Decorator PatternPhani Kumar - Decorator Pattern
Phani Kumar - Decorator Pattern
 

Semelhante a Observer and Decorator Pattern

Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
Vince Vo
 
Observer & singleton pattern
Observer  & singleton patternObserver  & singleton pattern
Observer & singleton pattern
babak danyal
 
Design patterns
Design patternsDesign patterns
Design patterns
ISsoft
 
Concurrent talk
Concurrent talkConcurrent talk
Concurrent talk
rahulrevo
 
Assignment Details There is a .h file on Moodle that provides a defi.pdf
Assignment Details There is a .h file on Moodle that provides a defi.pdfAssignment Details There is a .h file on Moodle that provides a defi.pdf
Assignment Details There is a .h file on Moodle that provides a defi.pdf
jyothimuppasani1
 

Semelhante a Observer and Decorator Pattern (20)

33071-AOOP-Exp6 (2).pdf
33071-AOOP-Exp6 (2).pdf33071-AOOP-Exp6 (2).pdf
33071-AOOP-Exp6 (2).pdf
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Observer & singleton pattern
Observer  & singleton patternObserver  & singleton pattern
Observer & singleton pattern
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
 
Sdp
SdpSdp
Sdp
 
Meteoio Introduction given by Mathias Bavey in Bozen
Meteoio Introduction given by Mathias Bavey in BozenMeteoio Introduction given by Mathias Bavey in Bozen
Meteoio Introduction given by Mathias Bavey in Bozen
 
Design patterns
Design patternsDesign patterns
Design patterns
 
The Singleton Pattern In Java
The Singleton Pattern In JavaThe Singleton Pattern In Java
The Singleton Pattern In Java
 
QTP Automation Testing Tutorial 7
QTP Automation Testing Tutorial 7QTP Automation Testing Tutorial 7
QTP Automation Testing Tutorial 7
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Java22_1670144363.pptx
Java22_1670144363.pptxJava22_1670144363.pptx
Java22_1670144363.pptx
 
Effective Java. By materials of Josch Bloch's book
Effective Java. By materials of Josch Bloch's bookEffective Java. By materials of Josch Bloch's book
Effective Java. By materials of Josch Bloch's book
 
Handling Lifecycles in a Jetpack way
Handling Lifecycles in a Jetpack wayHandling Lifecycles in a Jetpack way
Handling Lifecycles in a Jetpack way
 
Concurrent talk
Concurrent talkConcurrent talk
Concurrent talk
 
Pointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
Pointer Events Working Group update / TPAC 2023 / Patrick H. LaukePointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
Pointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
 
Assignment Details There is a .h file on Moodle that provides a defi.pdf
Assignment Details There is a .h file on Moodle that provides a defi.pdfAssignment Details There is a .h file on Moodle that provides a defi.pdf
Assignment Details There is a .h file on Moodle that provides a defi.pdf
 
Data binding in AngularJS, from model to view
Data binding in AngularJS, from model to viewData binding in AngularJS, from model to view
Data binding in AngularJS, from model to view
 
Vigneshram murugan cloud project_documentation
Vigneshram murugan cloud project_documentationVigneshram murugan cloud project_documentation
Vigneshram murugan cloud project_documentation
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Observer and Decorator Pattern

  • 1. Design Patterns 05/28/10 Week 5: Observer and Decorator Jonathan Simon [email_address]
  • 2.
  • 3.
  • 4.
  • 5. Interfaces 05/28/10 Contains signatures for registering, removing, and notifying observers. Contains Update method so that information can be sent from Subject to Observer.
  • 6. WeatherData (Subject) Questions: Which field is used for storing the list of Observers? How does an Observer add itself to this list? The fields for humidity, pressure, and temperature represent the state of the Subject.
  • 7.
  • 8.
  • 9. Current Conditions Display (Observer) Note that the individual Observer is composed of the Subject. So the Subjects has a reference to all Observers…and each Observer has a reference to a Subject. Questions: What method on this Observer is called by the Subject when data is changed?
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21. So far… 05/28/10 NotifyObserver will loop through each Observer and call Update() and pass a reference of itself Observer registers itself with the Subject
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27. The Big Picture 05/28/10
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Subject (Current Implementation) 05/28/10 Our Subject class contains the one notification event. Our ConcreteSubject gets the notification event by inheritance. We want our ConcreteSubject to contain multiple events. Does Inheritance make sense??? Would could we use instead?
  • 34. Subject/SubjectEvent 05/28/10 This class represents a specific event of the Subject.
  • 35. SubjectEvent 05/28/10 Now the SubjectEvent, not Subject, contains a reference to all of the Observers who are interested in being notified.
  • 36. Subject/SubjectEvent 05/28/10 The constructor of WeatherData takes a SubjectEvent as parameter.
  • 37.
  • 38.
  • 39. SubjectEvent 05/28/10 Added name parameter to the constructor and storing it in eventName Field.
  • 40.
  • 41.
  • 42.
  • 43.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.

Notas do Editor

  1. 13 November 2008
  2. Interface breaks. By value. The Observer needs to have it’s own version of the state data. Problem is that the Subject sends a lot more information that needed. We can resolve by sending a null value for data that has not changed. 13 November 2008
  3. 1. No. We would need to cast the subject to a specific subject so that we can get the properties that we want. This ties our Observer to a very specific Subject. Note with the Push model, the Observer did not need to know about the Subject data. 13 November 2008
  4. 1. reigsterObserver removeObserver notifyObservers 2. ArrayList observers 3. Initialize the ArrayList of observers 13 November 2008
  5. Temperature, humidity, and pressure aren’t part of a generic subject. 13 November 2008
  6. The Subject passed on a reference to itself. The SubjectEvent needs to pass the Subject as well. The SubjectEvent needs to tell the Observer the name of the event. 13 November 2008
  7. Observer = Search Screen. Just one observer Multiple Subjects 13 November 2008
  8. Observers = Each Child Node Subject = Parent Node 13 November 2008