You are on page 1of 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