Weather Station :

When the observer design pattern is used for the weather station, if there is an update, all the observers (Concrete Observer objects) are notified by using the Concrete Subject object. Also, new observers are added or removed by using the Concrete Subject object.

In the main class, the WeatherData object is created. Later, Concrete Observer objects such as CurrentConditionsDisplay, StatisticsDisplay, and ForecastDisplay are created with the WeatherData object passed as an argument in their constructors and these objects are added to the ArrayList variable of the WeatherData object. Calling the set method of the WeatherData object from the main class, necessary data are set, and make the update method with passing the Subject object parameter call. The variables of each Concrete Observer object are updated. By calling the detach method of the WeatherData object with passing the argument of a Concrete Observer object in the main class, makes the Concrete Observer remove from the ArrayList variable of the WeatherData object.

When the visitor design pattern is used for weather station, in the main method of Client class; CurrentConditionDisplay, StatisticsDisplay and ForecastDisplay objects are created in an array of Display object. Then, DisplayVisitor object is created and passed as a parameter as a parameter for each of the Display item's accept method call. In the accept method of Element ( CurrentConditionDisplay, StatisticsDisplay or ForecastDisplay ) class, visitor method of DisplayVisitorInterface is called by passing the related Element object as a parameter.

The generated java code of the weather station case study is below.

Observer-Visitor.pdf