You are here:   ArielOrtiz.info > Software Design and Architecture > Lab 3: Observer Pattern

Lab 3: Observer Pattern

Objectives

During this lab session:

This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

Activity Description

IMPORTANT NOTE: The lab activities can be developed individually or in pairs. The lab report must be developed individually.

  1. Create a folder called observer. Inside this folder, create two files called: weather.rb and tc_weather.rb.

    Both Ruby source files must start with a comment containing the lab's title, date, and the authors' personal information. For example:

    # Lab 3: Observer Pattern
    # Date: 11-Sep-2013
    # Authors:
    # 456654  Thursday Rubinstein 
    # 1160611 Anthony Stark
    
  2. You are required to build a weather data monitoring system using the Observer pattern (this example was taken from [FREEMAN] pp. 37-60). In the weather.rb source file, define four classes:

    • WeatherData: This is the subject of our Observer pattern implementation. Include as a mixin the Observable module and add to it a set_measurements method, which takes three parameters: temperature, humidity, and pressure. This method must notify all its observing objects that a change has occurred. This means that the update method for every observer must be indirectly called with the three previously mentioned parameters. Check chapter 5 of [OLSEN] and the Observable documentation.
    • CurrentConditionsDisplay: An observer that displays to the standard output the current weather conditions.
    • StatisticsDisplay: An observer that displays to the standard output weather statistics (average, maximum and minimum temperatures so far).
    • ForecastDisplay: An observer that displays to the standard output a weather forecast. If the current pressure is higher than the previous pressure, it predicts an improvement on the weather. If the current pressure is lower than the previous pressure, it predicts a cooler, rainy weather. Otherwise, it predicts that the weather will stay the same.

    Check the unit tests in the following step to see the expected display formats for each of the observers.

  3. Make sure the code that you write behaves exactly as expected by the following unit tests.

    # File: tc_weather.rb
    
    require 'test/unit'
    require 'stringio'
    require 'weather'
    
    class WeatherTest < Test::Unit::TestCase
    
      def setup
        @out = StringIO.new
        @old_stdout = $stdout
        $stdout = @out
        @weather_data = WeatherData.new
      end
    
      def teardown
        $stdout = @old_stdout
      end
    
      def do_set_measurements
        @weather_data.set_measurements(80.0, 65.0, 30.4)
        @weather_data.set_measurements(82.0, 70.0, 29.2)
        @weather_data.set_measurements(78.0, 90.0, 29.2)
      end
    
      def test_current_conditions_display
        current_display = CurrentConditionsDisplay.new
        @weather_data.add_observer(current_display)
        do_set_measurements
        assert_equal "Current conditions: 80.0F degrees and 65.0% humidity\n"  \
                     "Current conditions: 82.0F degrees and 70.0% humidity\n"  \
                     "Current conditions: 78.0F degrees and 90.0% humidity\n", \
                     @out.string
      end
    
      def test_statistics_display
        statistics_display = StatisticsDisplay.new
        @weather_data.add_observer(statistics_display)
        do_set_measurements
        assert_equal "Avg/Max/Min temperature = 80.0/80.0/80.0\n"  \
                     "Avg/Max/Min temperature = 81.0/82.0/80.0\n"  \
                     "Avg/Max/Min temperature = 80.0/82.0/78.0\n", \
                     @out.string
      end
    
      def test_forecast_display
        forecast_display = ForecastDisplay.new
        @weather_data.add_observer(forecast_display)
        do_set_measurements
        assert_equal "Forecast: Improving weather on the way!\n"       \
                     "Forecast: Watch out for cooler, rainy weather\n" \
                     "Forecast: More of the same\n",                   \
                     @out.string
      end
    
      def test_all_together
        current_display = CurrentConditionsDisplay.new
        statistics_display = StatisticsDisplay.new
        forecast_display = ForecastDisplay.new
        @weather_data.add_observer(current_display)
        @weather_data.add_observer(statistics_display)
        @weather_data.add_observer(forecast_display)
        do_set_measurements
        assert_equal "Current conditions: 80.0F degrees and 65.0% humidity\n" \
                     "Avg/Max/Min temperature = 80.0/80.0/80.0\n"             \
                     "Forecast: Improving weather on the way!\n"              \
                     "Current conditions: 82.0F degrees and 70.0% humidity\n" \
                     "Avg/Max/Min temperature = 81.0/82.0/80.0\n"             \
                     "Forecast: Watch out for cooler, rainy weather\n"        \
                     "Current conditions: 78.0F degrees and 90.0% humidity\n" \
                     "Avg/Max/Min temperature = 80.0/82.0/78.0\n"             \
                     "Forecast: More of the same\n",                          \
                     @out.string
      end
      
    end
    
  4. Deliverables

    To hand in your individual lab work, follow these instructions.

    Due date is Tuesday, September 17.

    Evaluation

    This activity will be evaluated using the following criteria:

    50% Implementation of functional requirements.
    50% Lab report.
    DA The program and/or report was plagiarized.