Last active
April 7, 2021 18:27
-
-
Save drhanlau/7c41f9a7e1c9ab3d7dc8 to your computer and use it in GitHub Desktop.
Code for Factory Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == '__main__': | |
domain = 'ftp.freebsd.org' | |
path = '/pub/FreeBSD/' | |
protocol = input('Connecting to {}. Which Protocol to use? (0-http,1-ftp): '.format(domain)) | |
if protocol == 0: | |
is_secure = bool(input('Use secure connection? (1-yes, 0-no): ')) | |
connector = HTTPConnector(is_secure) | |
else: | |
is_secure = False | |
connector = FTPConnector(is_secure) | |
try: | |
content = connector.read(domain, path) | |
except urllib2.URLError, e: | |
print 'Can not access resource with this method' | |
else: | |
print connector.parse(content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HTTPConnector(Connector): | |
"""A concrete creator that creates a HTTP connector and sets in | |
runtime all its attributes.""" | |
def protocol_factory_method(self): | |
if self.is_secure: | |
return 'https' | |
return 'http' | |
def port_factory_method(self): | |
"""Here HTTPPort and HTTPSecurePort are concrete objects, created by factory method.""" | |
if self.is_secure: | |
return HTTPSecurePort() | |
return HTTPPort() | |
def parse(self, content): | |
"""Parses web content.""" | |
filenames = [] | |
soup = BeautifulStoneSoup(content) | |
links = soup.table.findAll('a') | |
for link in links: | |
filenames.append(link['href']) | |
return '\n'.join(filenames) | |
class FTPConnector(Connector): | |
"""A concrete creator that creates a FTP connector and sets in | |
runtime all its attributes.""" | |
def protocol_factory_method(self): | |
return 'ftp' | |
def port_factory_method(self): | |
return FTPPort() | |
def parse(self, content): | |
lines = content.split('\n') | |
filenames = [] | |
for line in lines: | |
# The FTP format typically has 8 columns | |
splitted_line = line.split(None, 8) | |
if len(splitted_line) == 9: | |
filenames.append(splitted_line[-1]) | |
return '\n'.join(filenames) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@abc.abstractmethod | |
def parse(self): | |
"""Parses web content. | |
This method should be redefined in the runtime.""" | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Port(object): | |
__metaclass__ = abc.ABCMeta | |
"""Abstract product. One of its subclasses will be created in factory methods.""" | |
@abc.abstractmethod | |
def __str__(self): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment