Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Implementing Weather Forecast using Facade Design Pattern in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Facade Design Patterns are design patterns in Python that provide a simple interface to a complex subsystem. When we look into the world around us, we can always find facade design patterns. An automobile is the best example: you don’t need to understand how the engine functions. To operate the engine, you are provided with a set of simple interfaces such as turn the key in the ignition to start an engine, making use of steering wheels to turn the wheels, and more. Let’s look into the advantages of the Facade Design Pattern. They are as follows:

  • Maintains loose coupling between client and subsystems.
  • Easier to use and test
  • Provides a better and clearer API for the client code
  • It wraps a complicated subsystem with a simple interface

Weather Forecast Implementation

Let’s get into the implementation section. In this application, we fetch the current temperature in a city. For this purpose, we use available API’s from resources. Here, without using the facade design pattern, the client has to go through so many complicated tasks such as API call, parse and retrieve the necessary data, and converts from Kelvin to Celsius. It is easier If the client needs to call only one method to get the current temperature. That’s what we achieve through the facade design pattern.

Before getting into the source code, let’s look into the code design pattern.

In the above diagram, we have a subsystem consist of four modules. If the client, uses these modules directly, it will lose its flexibility. This is because, if any of the modules is changed, then the client code needs to be modified, which adds up additional complexities. While using a facade design pattern, the client uses a facade, where the facade calls the submodule and returns the necessary details to the client. This design is based on the principle of decomposition, where a complex system is split into smaller and simpler subsystems.

Let’s look into the functionality of each module. 

  • The WeatherRetriever class makes requests to the weather API endpoint and returns forecast data.
  • The Crawler class crawls the data returned from WeatherRetriever class, decodes it in the JSON format, and fetches the temperature.
  • The WeatherForecast class is responsible for storing the temperature data.
  • Finally, the Converter class converts the temperature from Kelvin to Celsius


import urllib
import urllib.request
import urllib.parse
import json
class WeatherRetriever(object):
    def __init__(self):
        self.api_url = '\
    # takes input (city and country), produces a URL string,
    # generate HTTP request, and returns the data.
    def fetch_weather_data(self, city, country):
        city = urllib.parse.quote(city)
        url = self.api_url.format(city, country)
        return urllib.request.urlopen(url).read()
class Crawler(object):
    def crawl_weather_data(self, weather_data):
        data = json.loads(weather_data)
        # fetch the current temperature
        temp_data = data['main']['temp']
        return temp_data
class Converter(object):
    """ converts the temperature from kelvin to celsius """
    def from_kelvin_to_celsius(self, kelvin):
        return kelvin - 273.15
class WeatherForecast(object):
    def __init__(self, data):
        self.temperature = data
class FacadeDesign(object):
    def get_forecast(self, city, country):
        weather_retriever = WeatherRetriever()
        weather_data = weather_retriever.fetch_weather_data(city, country)
        crawl = Crawler()
        crawl_data = crawl.crawl_weather_data(weather_data)
        weather = WeatherForecast(crawl_data)
        converter = Converter()
        temp_celsius = converter.from_kelvin_to_celsius(weather.temperature)
        return temp_celsius
if __name__ == '__main__':
    facadeDesign = FacadeDesign()
    print("So today's forecast is about ",
          facadeDesign.get_forecast('London', 'uk'), "degrees")


So today’s forecast is about  7.170000000000016 degrees

A facade design pattern provides a higher-level interface that simplifies the usage of subsystems. The facade design doesn’t add any new functionality, instead, it acts as a wrapper that provides flexibility in accessing complex subsystems.

My Personal Notes arrow_drop_up
Last Updated : 24 Jan, 2022
Like Article
Save Article
Similar Reads
Related Tutorials