Accessing Web Resources using Factory Method Design Pattern in Python

The Factory Method Design Pattern is a creational pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. This pattern helps define a generic interface for creating objects while allowing subclasses to decide which class to instantiate. In this article, we will explore how to use the Factory Method Design Pattern to access web resources in Python.

Why Use Factory Method for Web Resources?

Python provides several libraries for accessing web resources such as requests and urllib. These libraries allow us to send HTTP requests to a server and receive responses. However, choosing the right library can be challenging, especially when working on large projects with multiple dependencies. The Factory Method Design Pattern creates a common interface for accessing web resources and lets subclasses decide which library to use.

Creating the Abstract Factory

Let's start by defining the abstract class for our web resource factory ?

from abc import ABC, abstractmethod

class WebResourceFactory(ABC):

    @abstractmethod
    def get(self, url):
        pass

    @abstractmethod
    def post(self, url, data):
        pass

The WebResourceFactory class is an abstract class that defines the interface for accessing web resources. It has two abstract methods: get() and post(), which will be implemented by the concrete subclasses.

Implementing Concrete Factories

Next, let's create two concrete subclasses that implement the WebResourceFactory interface using the requests and urllib libraries ?

import urllib.request
import requests
from abc import ABC, abstractmethod

class WebResourceFactory(ABC):

    @abstractmethod
    def get(self, url):
        pass

    @abstractmethod
    def post(self, url, data):
        pass

class RequestsWebResourceFactory(WebResourceFactory):

    def get(self, url):
        return requests.get(url)

    def post(self, url, data):
        return requests.post(url, data)

class UrllibWebResourceFactory(WebResourceFactory):

    def get(self, url):
        response = urllib.request.urlopen(url)
        return response.read()

    def post(self, url, data):
        data = data.encode('utf-8')
        req = urllib.request.Request(url, data=data)
        response = urllib.request.urlopen(req)
        return response.read()

# Example usage
def access_web_resource(factory, url):
    web_resource = factory.get(url)
    print(f"Response type: {type(web_resource)}")
    return web_resource

# Using Requests library
requests_factory = RequestsWebResourceFactory()
response = access_web_resource(requests_factory, 'https://httpbin.org/get')

# Using urllib library
urllib_factory = UrllibWebResourceFactory()
data = access_web_resource(urllib_factory, 'https://httpbin.org/get')
Response type: <class 'requests.models.Response'>
Response type: <class 'bytes'>

The RequestsWebResourceFactory and UrllibWebResourceFactory classes are concrete subclasses that implement the WebResourceFactory interface using different libraries respectively.

POST Request Example

Here's an example demonstrating POST requests with both factories ?

import urllib.request
import requests
from abc import ABC, abstractmethod

class WebResourceFactory(ABC):

    @abstractmethod
    def get(self, url):
        pass

    @abstractmethod
    def post(self, url, data):
        pass

class RequestsWebResourceFactory(WebResourceFactory):

    def get(self, url):
        return requests.get(url)

    def post(self, url, data):
        return requests.post(url, data)

class UrllibWebResourceFactory(WebResourceFactory):

    def get(self, url):
        response = urllib.request.urlopen(url)
        return response.read()

    def post(self, url, data):
        data = data.encode('utf-8')
        req = urllib.request.Request(url, data=data)
        response = urllib.request.urlopen(req)
        return response.read()

# POST request example
def post_data(factory, url, data):
    response = factory.post(url, data)
    print(f"POST response type: {type(response)}")
    return response

# Using different factories for POST
test_data = "name=John&age=30"
test_url = "https://httpbin.org/post"

requests_factory = RequestsWebResourceFactory()
post_data(requests_factory, test_url, test_data)

urllib_factory = UrllibWebResourceFactory()  
post_data(urllib_factory, test_url, test_data)
POST response type: <class 'requests.models.Response'>
POST response type: <class 'bytes'>

Benefits of This Pattern

The Factory Method pattern provides several advantages when accessing web resources:

  • Flexibility: Easy to switch between different HTTP libraries
  • Maintainability: Changes to implementation don't affect client code
  • Extensibility: New HTTP libraries can be added without modifying existing code
  • Consistency: Uniform interface regardless of underlying library

Comparison

Factory Library Response Type Best For
RequestsWebResourceFactory requests Response object Advanced HTTP features
UrllibWebResourceFactory urllib bytes Standard library usage

Conclusion

The Factory Method Design Pattern provides a clean way to abstract web resource access in Python. This pattern allows you to switch between different HTTP libraries without changing client code, making your application more flexible and maintainable.

Updated on: 2026-03-27T07:10:01+05:30

252 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements