SE2811
Software Component Design

This is an old version of this course, from Winter 2013-2014. A newer version is avaliable here.

In this assignment, you will implement the Observer pattern in a multi-threaded application that receives weather updates from an Internet weather service application. Assignment

  • Download and review the domain independent Subject and Observer interfaces for the Observer pattern (observer.zip from the in-class code repository).
  • Download and execute the WeatherINET application (WeatherService.zip). Review the main() method as an example for the API. The application takes a query defining a location, e.g., New York, NY, and returns the current temperature in Celsius and Fahrenheit, wind speed, % humidity, and a 3-day forecast with URLs for icons.
  • The application consists of two parts: WeatherINET.java and WeatherXMLParser.java.
    • WeatherINET is used to retrieve an XML file as a String containing weather information for a given location. This is performed with the following method: public static String WeatherINET.makeQuery(String query).
    • WeatherXMLParser.java is a SAX parser used to parse and access the weather information.
    • Forecast.java is a wrapper class for holding forecast information for each day.
    • Example usage is shown below
  • Design concrete classes WeatherSubject and WeatherObserver for implementing the Subject and Observer interfaces for receiving weather updates. Your Observer pattern is required to maintain separate lists of observers for each requested location, i.e, use the Singleton pattern for managing distinct location registrations. You should be able to run WeatherService as a separate thread servicing multiple location requests. Your application should support multiple Observers for each location. WeatherSubject as one thread servicing multiple WeatherObserver threads requesting data services for the same or different locations.

Example usage

public static void main(String[] args) { 
   try { 
      System.out.println("Enter city: "); 

      // open up standard input 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
      String query = null; 
      while( (query = br.readLine()).length() > 0) { 
         WeatherXMLParser wxp = null; 
         String xml = WeatherINET.makeQuery(query); 
         if( xml.length() > 0) { 
            try { 
               wxp = new WeatherXMLParser(); 
               wxp.parseXML(xml); 
            } catch(Exception e) { 
               e.printStackTrace(); 
               System.out.println(e); 
            } 
            System.out.println(wxp.getHumidity()); 
            System.out.println("Icon: " +wxp.getIcon()); 
            System.out.println("Temp: " +wxp.getTemp_f() + " degF"); 
            System.out.println("Temp: " +wxp.getTemp_c() + " degC"); 
            System.out.println(wxp.getWind_condition()); 
            System.out.println(wxp.getCurrent_date_time()); 
            System.out.println(wxp.getForecastList()); 
            System.out.println("Enter city or return to exit: "); 
         } 
      } 
   } catch(IOException e) {
      e.printStackTrace();System.out.println(e);
   }
} 

Acknowledgement

This lab developed by Dr. Jay Urbain.