I am trying to improve the quality of my code by practising object oriented design patterns.
I have the following Python class that is responsible for connecting to a specified FTP server and downloading a resource from that server.
The "connect" function is the only function that will differ across different implementations since the login credentials and the server will be different.
class FtpResourceRetriever:
def __init__(self):
self.ftp = None
def connect(self):
# This implementation will be different for each concrete class
self.ftp = FTP()
def list_dir(self):
# Implementation will be identical across classes
self.ftp...
pass
def download_resource(self, resource_name: str):
# Implementation will be identical across classes
self.ftp...
pass
def quit(self):
# Implementation will be identical across classes
self.ftp...
pass
I considered using the Strategy pattern by having a "ConnectBehaviour" interface and then implementing concrete "ConnectBehaviour" classes; however, since I need to set self.ftp
in the "connect" method (which is a side-effect in the "FtpResourceRetriever" class), the Strategy approach doesn't feel entirely correct to me. Is this true?
What pattern should I be using in this case? Code examples would really go a long way in improving my understanding.
Aucun commentaire:
Enregistrer un commentaire