Last active
March 12, 2020 18:32
-
-
Save Biazus/4811c6b2f1c2a7c6b34f3bf8616d595d to your computer and use it in GitHub Desktop.
Sample of abstract factory by Miller Biazus
This file contains 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
#!/usr/bin/env python3 | |
from abc import ABC, abstractmethod | |
class AbstractFunction(ABC): | |
@abstractmethod | |
def install_function(self): | |
pass | |
class AbstractApplication(ABC): | |
@abstractmethod | |
def install_application(self): | |
pass | |
class EdgeFunction(AbstractFunction): # concrete class | |
def verbose(self): | |
return "Edge Function" | |
def install_function(self): | |
return f"installing edge function" | |
class MarketplaceFunction(AbstractFunction): # another concrete class | |
def verbose(self): | |
return "Marketplace Function" | |
def install_function(self): | |
return f"installing marketplace function" | |
class EdgeApplication(AbstractApplication): | |
def install_application(self, function): | |
function_str = function.verbose() | |
return f"installing edge application collaborating with {function_str}" | |
class MarketplaceApplication(AbstractApplication): | |
def install_application(self, function): | |
function_str = function.verbose() | |
return f"installing marketplace application collaborating with {function_str}" | |
class AbstractFactory(ABC): | |
# Just a couple of factory methods | |
# Note that if we need to extend our factory by creating new products it would require a chege here and also in | |
# all subclasses | |
@abstractmethod | |
def create_function(self): | |
pass | |
@abstractmethod | |
def create_application(self): | |
pass | |
class EdgeFactory(AbstractFactory): | |
# Responsibility of instance creation is kept under the concrete classes | |
def create_function(self): | |
return EdgeFunction() | |
def create_application(self): | |
return EdgeApplication() | |
class MarketplaceFactory(AbstractFactory): | |
def create_function(self): | |
return MarketplaceFunction() | |
def create_application(self): | |
return MarketplaceApplication() | |
def create_products(factory): # client code that builds Azion platform receive a factory as argument | |
new_function = factory.create_function() | |
new_application = factory.create_application() | |
print(new_function.install_function()) | |
print(new_application.install_application(new_function)) | |
create_products(EdgeFactory()) | |
create_products(MarketplaceFactory()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment