English 中文(简体)
Python Design Patterns - Proxy
  • 时间:2024-11-03

Python Design Patterns - Proxy


Previous Page Next Page  

The proxy design pattern includes a new object, which is called “Proxy” in place of an existing object which is called the “Real Subject”. The proxy object created of the real subject must be on the same interface in such a way that the cpent should not get any idea that proxy is used in place of the real object. Requests generated by the cpent to the proxy are passed through the real subject.

The UML representation of proxy pattern is as follows −

Proxy Pattern

How to implement the proxy pattern?

Let us now see how to implement the proxy pattern.

class Image:
   def __init__( self, filename ):
      self._filename = filename
   
   def load_image_from_disk( self ):
      print("loading " + self._filename )
   
   def display_image( self ):
      print("display " + self._filename)

class Proxy:
   def __init__( self, subject ):
      self._subject = subject
      self._proxystate = None

class ProxyImage( Proxy ):
   def display_image( self ):
      if self._proxystate == None:
         self._subject.load_image_from_disk()
         self._proxystate = 1
      print("display " + self._subject._filename )

proxy_image1 = ProxyImage ( Image("HiRes_10Mb_Photo1") )
proxy_image2 = ProxyImage ( Image("HiRes_10Mb_Photo2") )

proxy_image1.display_image() # loading necessary
proxy_image1.display_image() # loading unnecessary
proxy_image2.display_image() # loading necessary
proxy_image2.display_image() # loading unnecessary
proxy_image1.display_image() # loading unnecessary

Output

The above program generates the following output −

Proxy Pattern Output

The proxy pattern design helps in reppcating the images that we created. The display_image() function helps to check if the values are getting printed in the command prompt.

Advertisements