使用 Python 工廠方法設計模式訪問 Web 資源


Python 的工廠方法設計模式是一種建立型模式,它在超類中提供建立物件的介面,但允許子類更改將要建立的物件的型別。它用於定義建立物件的通用介面,同時允許子類決定例項化哪個類。在本文中,我們將探討如何使用工廠方法設計模式在 Python 中訪問 Web 資源。

訪問 Web 資源

Python 提供了多個用於訪問 Web 資源的庫,例如 HTTP 請求、urllib 和 Requests。這些庫允許我們向伺服器傳送 HTTP 請求並接收響應。但是,選擇合適的庫可能會很困難,尤其是在處理具有多個依賴項的大型專案時。為了解決這個問題,我們可以使用工廠方法設計模式來建立一個訪問 Web 資源的通用介面,並讓子類決定使用哪個庫。

讓我們從為我們的工廠定義抽象類開始:

from abc import ABC, abstractmethod

class WebResourceFactory(ABC):

   @abstractmethod
   def get(self, url):
      pass

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

WebResourceFactory 類是一個抽象類,它定義了訪問 Web 資源的介面。它有兩個抽象方法:get() 和 post(),這些方法將由具體的子類實現。

接下來,讓我們建立兩個具體的子類,它們分別使用 Requests 和 urllib 庫實現 WebResourceFactory 介面:

import urllib.request
import requests

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()

RequestsWebResourceFactory 和 UrllibWebResourceFactory 類是具體的子類,它們分別使用 Requests 和 urllib 庫實現 WebResourceFactory 介面。

現在,我們可以使用 WebResourceFactory 介面統一訪問 Web 資源,而無需擔心底層庫。在下面的程式碼中,我們訪問 Google 首頁的 Web 資源。

access_web_resource() 函式接受一個工廠物件和一個 URL,並使用工廠物件訪問指定 URL 上的 Web 資源。我們可以使用 RequestsWebResourceFactory 或 UrllibWebResourceFactory 類建立一個工廠物件,具體取決於我們想要使用的庫。

def access_web_resource(factory, url):
    web_resource = factory.get(url)
    print(web_resource)

factory = RequestsWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

factory = UrllibWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

完整的程式碼和訪問 Google 首頁的輸出如下:

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()

def access_web_resource(factory, url):
   web_resource = factory.get(url)
   print(web_resource)

factory = RequestsWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

factory = UrllibWebResourceFactory()
access_web_resource(factory, 'https://www.google.com')

輸出

<Response [200]>
b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" 
lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-
equiv="Content-Type"><meta 
content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" 
itemprop="image"><title>Google</title><script …

結論

在本文中,我們探討了如何使用工廠方法設計模式在 Python 中使用 Requests 和 urllib 庫建立一個訪問 Web 資源的通用介面。但是,此模式可以應用於任何需要根據通用介面建立不同型別物件的場景。

更新於:2023年7月6日

瀏覽量:116

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.