0% found this document useful (0 votes)
40 views

Design Patterns: Observer Pattern Tutorial

The document discusses the observer design pattern and how it adheres to design principles like separation of concerns and programming to interfaces. It also provides an example of how to implement an observer pattern in a weather application by downloading code, filling in incomplete classes like WeatherData and the observer displays, and writing code in the WeatherStation class to connect the components and allow subject notification.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Design Patterns: Observer Pattern Tutorial

The document discusses the observer design pattern and how it adheres to design principles like separation of concerns and programming to interfaces. It also provides an example of how to implement an observer pattern in a weather application by downloading code, filling in incomplete classes like WeatherData and the observer displays, and writing code in the WeatherStation class to connect the components and allow subject notification.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Design Patterns

Observer Pattern Tutorial


Design Principles
• For each of following design principles, describe how the observer
pattern makes use of that principle
• Identify the aspects of your application that vary and separate them from what
stays the same
• What varies in the observer pattern is the state of the Subject, and number and type of
Observers
• We can add the objects that are dependent on the Subject without changing the Subject
• Program to an interface, not an implementation
• Both Subject and Observer use interfaces
• Subject keeps track of objects implementing the Observer interface
• Observer can register/deregister, and get notified by the Subject
• Favor composition over inheritance
• Observers register themselves using composition
Weather App Design using Observer Pattern
Tutorial
• Download the code from LMS
• Create new java project, name it Observer
• Use the classes you downloaded from LMS
• For some classes the code is not complete, fill in the code based on the
comment inside the class
• WeatherData class
• Update the code of the three observers (Displays)
• Finally, In the WeatherStation class, write the code to connect all components
together
• modify the code to allow notification using pull approach and test it in the
WeatherStation class

You might also like