Observer Design Pattern
This is a one of the famous behavioral design patterns in
the object-oriented world.
- Why we need observer design pattern?
- What problem it solves?
In order to answer those questions, we need to analysis the
software design & development practical scenarios.So lets closely looks in to below scenario:
- Develop a software by WITHOUT applying observer pattern
- Develop a software by applying observer pattern
After the analysis, you can answer to above questions by yourself
as well as you could get an idea where to apply this pattern in day to day
software development projects.
Let’s continue the Weather Station Story......
As per the agreement John successfully completed the data
feeding application using RMI. But still Volta (weather forecasting company) use paper based reports to presented their analysis reports and those reports print per
hourly basis. When weather data interested party (government, media, etc.)
wants to know the current weather condition or predictions, they need to
contact Volta's telephone operator through telephone. Then telephone operator
read data from the printed reports and respond to caller.
![]() |
(Image source: Iron Age Tattoo)
|
Volta management wanted to optimize this process. So, they
decided to mount 2 big displays in the front office to display current weather
conditions and foretasted results. Management expected to automatically update displays
as soon as automated analysis completed. Then telephone operator can easily
present with up to date forecasting results.
Due to successful of previous project, Volta management decided
to give this project to One Soft Solutions. But this time One Soft Solutions
senior engineer (John) was getting his annual leave. After a One Soft Solutions
management discussion through telephone call John agreed to give some
guidelines to his junior developers to continue the project until he come. As
promised John, he ask to do this job for one of his junior team member (Devid).
John prepared a task sheet and share with his Devid to
fallow.
- Assign the two front end UI design & development to two separate teams (Team A, Team B)
- Create a new method called “updateDisplay” in “WeatherStation.java class” and write code to update the 2 remote displays which will develop by two UI teams.
- Call updateDisplay method at the end of data analysis code then it will update the displays accordingly.
Devid discuss with rest of the team and draw a diagram to
illustrate what they are going to do.
| Figure 1.1 communication workflow |
Team A – Developed the “CurrentConditionDisplay”
Team A used RMI to expose their front end object to 'WeatherStation'.Then WeatherStation can remotely invoke the UI object to update the display board (More details of RMI :RMI Post).
They had used "Display" interface.
| Figure 1.3 Remote Interface For Current Condition Display |
Team B – Developed the “ForecastConditionDisplay”
They also followed the same process (Team A's).
| Figure 1.4 Remote Interface For Forecast Condition Display |
Meanwhile Devid and team had developed the WeatherStation back-end code base.They have retrieved the remote display objects and called the "exposeDataToDisplays" method to update remote objects status.
![]() |
| Figure 1.5 Remote Object Update |
Seems like everything is ok. All the client expected functions are in there.
After two weeks....
John : Hi Devid ..I reviewed our weather station program
Devid : Aha..We did that before expected date
John : Yes..You guys did it well..But currently display clients are tightly couple to our code.Right....
Devid : Yeah..
John : What is the possibility to add new Display type with minimum changes to existing code.
Devid : I understood the problem..But I don't have a clear idea how to do that.
John :
- You could create common interface ("Display.java")
- Ask all the UI teams to use that interface as their remote interface.
- Then you could create list of display clients.
- Add all the remote display client's object to that array list.
- Then you can update all the display objects in one-click.
Devid: What a cool thing..You decided it very well..
John : I proposed this solution by keeping "Observer Design Pattern" in mind.You also better to go through that design pattern and try to reduce coupling.I mean
"Check weather there is a way to expose weather data as API and clients could subscribe the weather service by themselves"


No comments:
Post a Comment